Skip to content

Commit ccf6124

Browse files
Fix erroneous span for borrowck error
1 parent 37a4225 commit ccf6124

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,20 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
357357
.add_element(live_region_vid, location);
358358
});
359359

360+
// HACK(compiler-errors): Constants that are gathered into Body.required_consts
361+
// have their locations erased...
362+
let locations = if location != Location::START {
363+
location.to_locations()
364+
} else {
365+
Locations::All(constant.span)
366+
};
367+
360368
if let Some(annotation_index) = constant.user_ty {
361369
if let Err(terr) = self.cx.relate_type_and_user_type(
362370
constant.literal.ty(),
363371
ty::Variance::Invariant,
364372
&UserTypeProjection { base: annotation_index, projs: vec![] },
365-
location.to_locations(),
373+
locations,
366374
ConstraintCategory::Boring,
367375
) {
368376
let annotation = &self.cx.user_type_annotations[annotation_index];
@@ -390,12 +398,9 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
390398
promoted: &Body<'tcx>,
391399
ty,
392400
san_ty| {
393-
if let Err(terr) = verifier.cx.eq_types(
394-
ty,
395-
san_ty,
396-
location.to_locations(),
397-
ConstraintCategory::Boring,
398-
) {
401+
if let Err(terr) =
402+
verifier.cx.eq_types(ty, san_ty, locations, ConstraintCategory::Boring)
403+
{
399404
span_mirbug!(
400405
verifier,
401406
promoted,
@@ -416,7 +421,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
416421
}
417422
} else {
418423
if let Err(terr) = self.cx.fully_perform_op(
419-
location.to_locations(),
424+
locations,
420425
ConstraintCategory::Boring,
421426
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
422427
constant.literal.ty(),
@@ -435,7 +440,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
435440
}
436441
} else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
437442
let unnormalized_ty = tcx.type_of(static_def_id);
438-
let locations = location.to_locations();
439443
let normalized_ty = self.cx.normalize(unnormalized_ty, locations);
440444
let literal_ty = constant.literal.ty().builtin_deref(true).unwrap().ty;
441445

@@ -454,7 +458,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
454458
self.cx.normalize_and_prove_instantiated_predicates(
455459
def_id,
456460
instantiated_predicates,
457-
location.to_locations(),
461+
locations,
458462
);
459463
}
460464
}

src/test/ui/hrtb/hrtb-just-for-static.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: implementation of `Foo` is not general enough
22
--> $DIR/hrtb-just-for-static.rs:24:5
33
|
44
LL | want_hrtb::<StaticInt>()
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
5+
| ^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
66
|
77
= note: `StaticInt` must implement `Foo<&'0 isize>`, for any lifetime `'0`...
88
= note: ...but it actually implements `Foo<&'static isize>`

src/test/ui/nll/issue-97997.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Foo {
2+
const ASSOC: bool = true;
3+
}
4+
impl<T> Foo for fn(T) {}
5+
6+
fn foo(_x: i32) {}
7+
8+
fn impls_foo<T: Foo>(_x: T) {}
9+
10+
fn main() {
11+
impls_foo(foo as fn(i32));
12+
13+
<fn(&u8) as Foo>::ASSOC;
14+
//~^ ERROR implementation of `Foo` is not general enough
15+
//~| ERROR implementation of `Foo` is not general enough
16+
}

src/test/ui/nll/issue-97997.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: implementation of `Foo` is not general enough
2+
--> $DIR/issue-97997.rs:13:5
3+
|
4+
LL | <fn(&u8) as Foo>::ASSOC;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
6+
|
7+
= note: `Foo` would have to be implemented for the type `for<'r> fn(&'r u8)`
8+
= note: ...but `Foo` is actually implemented for the type `fn(&'0 u8)`, for some specific lifetime `'0`
9+
10+
error: implementation of `Foo` is not general enough
11+
--> $DIR/issue-97997.rs:13:5
12+
|
13+
LL | <fn(&u8) as Foo>::ASSOC;
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
15+
|
16+
= note: `Foo` would have to be implemented for the type `for<'r> fn(&'r u8)`
17+
= note: ...but `Foo` is actually implemented for the type `fn(&'0 u8)`, for some specific lifetime `'0`
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)