From 37f5cc22391e693523e8320a1d5c214610790911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 2 Sep 2019 18:03:54 -0700 Subject: [PATCH 1/4] Do not complain about unconstrained params when Self is Ty Error --- .../constrained_generic_params.rs | 18 ++++++++---------- src/librustc_typeck/impl_wf_check.rs | 4 ++++ src/test/ui/issues/issue-36836.rs | 5 +++++ src/test/ui/issues/issue-36836.stderr | 9 +++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/issues/issue-36836.rs create mode 100644 src/test/ui/issues/issue-36836.stderr diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 79a04b9423a8e..c95f81506cd59 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -20,10 +20,10 @@ impl From for Parameter { } /// Returns the set of parameters constrained by the impl header. -pub fn parameters_for_impl<'tcx>(impl_self_ty: Ty<'tcx>, - impl_trait_ref: Option>) - -> FxHashSet -{ +pub fn parameters_for_impl<'tcx>( + impl_self_ty: Ty<'tcx>, + impl_trait_ref: Option>, +) -> FxHashSet { let vec = match impl_trait_ref { Some(tr) => parameters_for(&tr, false), None => parameters_for(&impl_self_ty, false), @@ -36,12 +36,10 @@ pub fn parameters_for_impl<'tcx>(impl_self_ty: Ty<'tcx>, /// uniquely determined by `t` (see RFC 447). If it is true, return the list /// of parameters whose values are needed in order to constrain `ty` - these /// differ, with the latter being a superset, in the presence of projections. -pub fn parameters_for<'tcx, T>(t: &T, - include_nonconstraining: bool) - -> Vec - where T: TypeFoldable<'tcx> -{ - +pub fn parameters_for<'tcx, T: TypeFoldable<'tcx>>( + t: &T, + include_nonconstraining: bool, +) -> Vec { let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining, diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index fcfd9adef54df..8e69fbd9a79ef 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -99,6 +99,10 @@ fn enforce_impl_params_are_constrained( ) { // Every lifetime used in an associated type must be constrained. let impl_self_ty = tcx.type_of(impl_def_id); + if impl_self_ty.sty == ty::Error { + // Don't complain about unconstrained type params when self ty doesn't exist. (#36836) + return; + } let impl_generics = tcx.generics_of(impl_def_id); let impl_predicates = tcx.predicates_of(impl_def_id); let impl_trait_ref = tcx.impl_trait_ref(impl_def_id); diff --git a/src/test/ui/issues/issue-36836.rs b/src/test/ui/issues/issue-36836.rs new file mode 100644 index 0000000000000..56d5a7cca4566 --- /dev/null +++ b/src/test/ui/issues/issue-36836.rs @@ -0,0 +1,5 @@ +trait Foo {} + +impl Foo for Bar {} //~ ERROR cannot find type `Bar` in this scope + +fn main() {} diff --git a/src/test/ui/issues/issue-36836.stderr b/src/test/ui/issues/issue-36836.stderr new file mode 100644 index 0000000000000..bfda9b0bbdd7e --- /dev/null +++ b/src/test/ui/issues/issue-36836.stderr @@ -0,0 +1,9 @@ +error[E0412]: cannot find type `Bar` in this scope + --> $DIR/issue-36836.rs:3:17 + | +LL | impl Foo for Bar {} + | ^^^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0412`. From 3cb1ed4279d8ca4528e3777d5470ca7dc85a976a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 2 Sep 2019 20:51:31 -0700 Subject: [PATCH 2/4] review comments --- src/librustc_typeck/constrained_generic_params.rs | 4 ++-- src/test/ui/issues/issue-36836.rs | 10 ++++++++++ src/test/ui/issues/issue-36836.stderr | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index c95f81506cd59..dd44f86717fe5 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -36,8 +36,8 @@ pub fn parameters_for_impl<'tcx>( /// uniquely determined by `t` (see RFC 447). If it is true, return the list /// of parameters whose values are needed in order to constrain `ty` - these /// differ, with the latter being a superset, in the presence of projections. -pub fn parameters_for<'tcx, T: TypeFoldable<'tcx>>( - t: &T, +pub fn parameters_for<'tcx>( + t: &impl TypeFoldable<'tcx>, include_nonconstraining: bool, ) -> Vec { let mut collector = ParameterCollector { diff --git a/src/test/ui/issues/issue-36836.rs b/src/test/ui/issues/issue-36836.rs index 56d5a7cca4566..99c56213153e4 100644 --- a/src/test/ui/issues/issue-36836.rs +++ b/src/test/ui/issues/issue-36836.rs @@ -1,3 +1,13 @@ +// Previously, in addition to the real cause of the problem as seen below, +// the compiler would tell the user: +// +// ``` +// error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or +// predicates +// ``` +// +// With this test, we check that only the relevant error is emitted. + trait Foo {} impl Foo for Bar {} //~ ERROR cannot find type `Bar` in this scope diff --git a/src/test/ui/issues/issue-36836.stderr b/src/test/ui/issues/issue-36836.stderr index bfda9b0bbdd7e..418194fac9923 100644 --- a/src/test/ui/issues/issue-36836.stderr +++ b/src/test/ui/issues/issue-36836.stderr @@ -1,5 +1,5 @@ error[E0412]: cannot find type `Bar` in this scope - --> $DIR/issue-36836.rs:3:17 + --> $DIR/issue-36836.rs:13:17 | LL | impl Foo for Bar {} | ^^^ not found in this scope From 87866714ee1cf3fba6e659f46413e361b9088362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 3 Sep 2019 08:12:28 -0700 Subject: [PATCH 3/4] fix comment and add delay_span_bug --- src/librustc_typeck/impl_wf_check.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index 8e69fbd9a79ef..82acbf7c74803 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -12,7 +12,7 @@ use crate::constrained_generic_params as cgp; use rustc::hir; use rustc::hir::itemlikevisit::ItemLikeVisitor; use rustc::hir::def_id::DefId; -use rustc::ty::{self, TyCtxt}; +use rustc::ty::{self, TyCtxt, TypeFoldable}; use rustc::ty::query::Providers; use rustc::util::nodemap::{FxHashMap, FxHashSet}; use std::collections::hash_map::Entry::{Occupied, Vacant}; @@ -99,8 +99,13 @@ fn enforce_impl_params_are_constrained( ) { // Every lifetime used in an associated type must be constrained. let impl_self_ty = tcx.type_of(impl_def_id); - if impl_self_ty.sty == ty::Error { - // Don't complain about unconstrained type params when self ty doesn't exist. (#36836) + if impl_self_ty.references_error() { + // Don't complain about unconstrained type params when self ty isn't known due to errors. + // (#36836) + tcx.sess.delay_span_bug(tcx.def_span(impl_def_id), &format( + "potentially unconstrained type parameters weren't evaluated on `{:?}`", + impl_self_ty, + )); return; } let impl_generics = tcx.generics_of(impl_def_id); From c44ffafab902e687ef01d2366a7de7237e25245c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 3 Sep 2019 08:33:06 -0700 Subject: [PATCH 4/4] review comment --- src/librustc_typeck/impl_wf_check.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index 82acbf7c74803..bc0f17c3bf0fb 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -102,10 +102,10 @@ fn enforce_impl_params_are_constrained( if impl_self_ty.references_error() { // Don't complain about unconstrained type params when self ty isn't known due to errors. // (#36836) - tcx.sess.delay_span_bug(tcx.def_span(impl_def_id), &format( - "potentially unconstrained type parameters weren't evaluated on `{:?}`", - impl_self_ty, - )); + tcx.sess.delay_span_bug( + tcx.def_span(impl_def_id), + "potentially unconstrained type parameters weren't evaluated", + ); return; } let impl_generics = tcx.generics_of(impl_def_id);