From ccf6124bd972ed2b0995f98a86554d201b8ac5c7 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 12 Jun 2022 02:35:49 -0700 Subject: [PATCH] Fix erroneous span for borrowck error --- compiler/rustc_borrowck/src/type_check/mod.rs | 24 +++++++++++-------- src/test/ui/hrtb/hrtb-just-for-static.stderr | 2 +- src/test/ui/nll/issue-97997.rs | 16 +++++++++++++ src/test/ui/nll/issue-97997.stderr | 20 ++++++++++++++++ 4 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/nll/issue-97997.rs create mode 100644 src/test/ui/nll/issue-97997.stderr diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index e405baf7575..8075032c906 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -357,12 +357,20 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { .add_element(live_region_vid, location); }); + // HACK(compiler-errors): Constants that are gathered into Body.required_consts + // have their locations erased... + let locations = if location != Location::START { + location.to_locations() + } else { + Locations::All(constant.span) + }; + if let Some(annotation_index) = constant.user_ty { if let Err(terr) = self.cx.relate_type_and_user_type( constant.literal.ty(), ty::Variance::Invariant, &UserTypeProjection { base: annotation_index, projs: vec![] }, - location.to_locations(), + locations, ConstraintCategory::Boring, ) { let annotation = &self.cx.user_type_annotations[annotation_index]; @@ -390,12 +398,9 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { promoted: &Body<'tcx>, ty, san_ty| { - if let Err(terr) = verifier.cx.eq_types( - ty, - san_ty, - location.to_locations(), - ConstraintCategory::Boring, - ) { + if let Err(terr) = + verifier.cx.eq_types(ty, san_ty, locations, ConstraintCategory::Boring) + { span_mirbug!( verifier, promoted, @@ -416,7 +421,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { } } else { if let Err(terr) = self.cx.fully_perform_op( - location.to_locations(), + locations, ConstraintCategory::Boring, self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new( constant.literal.ty(), @@ -435,7 +440,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { } } else if let Some(static_def_id) = constant.check_static_ptr(tcx) { let unnormalized_ty = tcx.type_of(static_def_id); - let locations = location.to_locations(); let normalized_ty = self.cx.normalize(unnormalized_ty, locations); let literal_ty = constant.literal.ty().builtin_deref(true).unwrap().ty; @@ -454,7 +458,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { self.cx.normalize_and_prove_instantiated_predicates( def_id, instantiated_predicates, - location.to_locations(), + locations, ); } } diff --git a/src/test/ui/hrtb/hrtb-just-for-static.stderr b/src/test/ui/hrtb/hrtb-just-for-static.stderr index a5770431eaf..b4312091edb 100644 --- a/src/test/ui/hrtb/hrtb-just-for-static.stderr +++ b/src/test/ui/hrtb/hrtb-just-for-static.stderr @@ -2,7 +2,7 @@ error: implementation of `Foo` is not general enough --> $DIR/hrtb-just-for-static.rs:24:5 | LL | want_hrtb::() - | ^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough + | ^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough | = note: `StaticInt` must implement `Foo<&'0 isize>`, for any lifetime `'0`... = note: ...but it actually implements `Foo<&'static isize>` diff --git a/src/test/ui/nll/issue-97997.rs b/src/test/ui/nll/issue-97997.rs new file mode 100644 index 00000000000..c64e720b12f --- /dev/null +++ b/src/test/ui/nll/issue-97997.rs @@ -0,0 +1,16 @@ +trait Foo { + const ASSOC: bool = true; +} +impl Foo for fn(T) {} + +fn foo(_x: i32) {} + +fn impls_foo(_x: T) {} + +fn main() { + impls_foo(foo as fn(i32)); + + ::ASSOC; + //~^ ERROR implementation of `Foo` is not general enough + //~| ERROR implementation of `Foo` is not general enough +} diff --git a/src/test/ui/nll/issue-97997.stderr b/src/test/ui/nll/issue-97997.stderr new file mode 100644 index 00000000000..78401bbf654 --- /dev/null +++ b/src/test/ui/nll/issue-97997.stderr @@ -0,0 +1,20 @@ +error: implementation of `Foo` is not general enough + --> $DIR/issue-97997.rs:13:5 + | +LL | ::ASSOC; + | ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough + | + = note: `Foo` would have to be implemented for the type `for<'r> fn(&'r u8)` + = note: ...but `Foo` is actually implemented for the type `fn(&'0 u8)`, for some specific lifetime `'0` + +error: implementation of `Foo` is not general enough + --> $DIR/issue-97997.rs:13:5 + | +LL | ::ASSOC; + | ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough + | + = note: `Foo` would have to be implemented for the type `for<'r> fn(&'r u8)` + = note: ...but `Foo` is actually implemented for the type `fn(&'0 u8)`, for some specific lifetime `'0` + +error: aborting due to 2 previous errors +