Rollup merge of #106785 - compiler-errors:better-impl-wf-spans, r=estebank

Make blame spans better for impl wfcheck

r? types
This commit is contained in:
Yuki Okushi 2023-01-13 16:54:24 +09:00 committed by GitHub
commit 5adc7a5e37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 106 additions and 83 deletions

View File

@ -1254,7 +1254,11 @@ fn check_impl<'tcx>(
// therefore don't need to be WF (the trait's `Self: Trait` predicate
// won't hold).
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap();
let trait_ref = wfcx.normalize(ast_trait_ref.path.span, None, trait_ref);
let trait_ref = wfcx.normalize(
ast_trait_ref.path.span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
trait_ref,
);
let trait_pred = ty::TraitPredicate {
trait_ref,
constness: match constness {
@ -1263,7 +1267,7 @@ fn check_impl<'tcx>(
},
polarity: ty::ImplPolarity::Positive,
};
let obligations = traits::wf::trait_obligations(
let mut obligations = traits::wf::trait_obligations(
wfcx.infcx,
wfcx.param_env,
wfcx.body_id,
@ -1271,6 +1275,13 @@ fn check_impl<'tcx>(
ast_trait_ref.path.span,
item,
);
for obligation in &mut obligations {
if let Some(pred) = obligation.predicate.to_opt_poly_trait_pred()
&& pred.self_ty().skip_binder() == trait_ref.self_ty()
{
obligation.cause.span = ast_self_ty.span;
}
}
debug!(?obligations);
wfcx.register_obligations(obligations);
}

View File

@ -114,34 +114,46 @@ fn diagnostic_hir_wf_check<'tcx>(
// Get the starting `hir::Ty` using our `WellFormedLoc`.
// We will walk 'into' this type to try to find
// a more precise span for our predicate.
let ty = match loc {
let tys = match loc {
WellFormedLoc::Ty(_) => match hir.get(hir_id) {
hir::Node::ImplItem(item) => match item.kind {
hir::ImplItemKind::Type(ty) => Some(ty),
hir::ImplItemKind::Const(ty, _) => Some(ty),
hir::ImplItemKind::Type(ty) => vec![ty],
hir::ImplItemKind::Const(ty, _) => vec![ty],
ref item => bug!("Unexpected ImplItem {:?}", item),
},
hir::Node::TraitItem(item) => match item.kind {
hir::TraitItemKind::Type(_, ty) => ty,
hir::TraitItemKind::Const(ty, _) => Some(ty),
hir::TraitItemKind::Type(_, ty) => ty.into_iter().collect(),
hir::TraitItemKind::Const(ty, _) => vec![ty],
ref item => bug!("Unexpected TraitItem {:?}", item),
},
hir::Node::Item(item) => match item.kind {
hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _) => Some(ty),
hir::ItemKind::Impl(ref impl_) => {
assert!(impl_.of_trait.is_none(), "Unexpected trait impl: {:?}", impl_);
Some(impl_.self_ty)
}
hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _) => vec![ty],
hir::ItemKind::Impl(ref impl_) => match &impl_.of_trait {
Some(t) => t
.path
.segments
.last()
.iter()
.flat_map(|seg| seg.args().args)
.filter_map(|arg| {
if let hir::GenericArg::Type(ty) = arg { Some(*ty) } else { None }
})
.chain([impl_.self_ty])
.collect(),
None => {
vec![impl_.self_ty]
}
},
ref item => bug!("Unexpected item {:?}", item),
},
hir::Node::Field(field) => Some(field.ty),
hir::Node::Field(field) => vec![field.ty],
hir::Node::ForeignItem(ForeignItem {
kind: ForeignItemKind::Static(ty, _), ..
}) => Some(*ty),
}) => vec![*ty],
hir::Node::GenericParam(hir::GenericParam {
kind: hir::GenericParamKind::Type { default: Some(ty), .. },
..
}) => Some(*ty),
}) => vec![*ty],
ref node => bug!("Unexpected node {:?}", node),
},
WellFormedLoc::Param { function: _, param_idx } => {
@ -149,16 +161,16 @@ fn diagnostic_hir_wf_check<'tcx>(
// Get return type
if param_idx as usize == fn_decl.inputs.len() {
match fn_decl.output {
hir::FnRetTy::Return(ty) => Some(ty),
hir::FnRetTy::Return(ty) => vec![ty],
// The unit type `()` is always well-formed
hir::FnRetTy::DefaultReturn(_span) => None,
hir::FnRetTy::DefaultReturn(_span) => vec![],
}
} else {
Some(&fn_decl.inputs[param_idx as usize])
vec![&fn_decl.inputs[param_idx as usize]]
}
}
};
if let Some(ty) = ty {
for ty in tys {
visitor.visit_ty(ty);
}
visitor.cause

View File

@ -1,8 +1,8 @@
error[E0277]: `T` cannot be sent between threads safely
--> $DIR/builtin-superkinds-double-superkind.rs:6:24
--> $DIR/builtin-superkinds-double-superkind.rs:6:32
|
LL | impl <T: Sync+'static> Foo for (T,) { }
| ^^^ `T` cannot be sent between threads safely
| ^^^^ `T` cannot be sent between threads safely
|
= note: required because it appears within the type `(T,)`
note: required by a bound in `Foo`
@ -16,10 +16,10 @@ LL | impl <T: Sync+'static + std::marker::Send> Foo for (T,) { }
| +++++++++++++++++++
error[E0277]: `T` cannot be shared between threads safely
--> $DIR/builtin-superkinds-double-superkind.rs:9:16
--> $DIR/builtin-superkinds-double-superkind.rs:9:24
|
LL | impl <T: Send> Foo for (T,T) { }
| ^^^ `T` cannot be shared between threads safely
| ^^^^^ `T` cannot be shared between threads safely
|
= note: required because it appears within the type `(T, T)`
note: required by a bound in `Foo`

View File

@ -1,8 +1,8 @@
error[E0277]: `T` cannot be sent between threads safely
--> $DIR/builtin-superkinds-in-metadata.rs:13:23
--> $DIR/builtin-superkinds-in-metadata.rs:13:56
|
LL | impl <T:Sync+'static> RequiresRequiresShareAndSend for X<T> { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `T` cannot be sent between threads safely
| ^^^^ `T` cannot be sent between threads safely
|
note: required because it appears within the type `X<T>`
--> $DIR/builtin-superkinds-in-metadata.rs:9:8

View File

@ -1,8 +1,8 @@
error[E0277]: `Rc<i8>` cannot be sent between threads safely
--> $DIR/builtin-superkinds-simple.rs:6:6
--> $DIR/builtin-superkinds-simple.rs:6:14
|
LL | impl Foo for std::rc::Rc<i8> { }
| ^^^ `Rc<i8>` cannot be sent between threads safely
| ^^^^^^^^^^^^^^^ `Rc<i8>` cannot be sent between threads safely
|
= help: the trait `Send` is not implemented for `Rc<i8>`
note: required by a bound in `Foo`

View File

@ -1,8 +1,8 @@
error[E0277]: `T` cannot be sent between threads safely
--> $DIR/builtin-superkinds-typaram-not-send.rs:5:24
--> $DIR/builtin-superkinds-typaram-not-send.rs:5:32
|
LL | impl <T: Sync+'static> Foo for T { }
| ^^^ `T` cannot be sent between threads safely
| ^ `T` cannot be sent between threads safely
|
note: required by a bound in `Foo`
--> $DIR/builtin-superkinds-typaram-not-send.rs:3:13

View File

@ -1,8 +1,8 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/impl_wf.rs:11:6
--> $DIR/impl_wf.rs:11:14
|
LL | impl Foo for str { }
| ^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by a bound in `Foo`
@ -12,10 +12,10 @@ LL | trait Foo: Sized { }
| ^^^^^ required by this bound in `Foo`
error[E0277]: the trait bound `f32: Foo` is not satisfied
--> $DIR/impl_wf.rs:22:6
--> $DIR/impl_wf.rs:22:19
|
LL | impl Baz<f32> for f32 { }
| ^^^^^^^^ the trait `Foo` is not implemented for `f32`
| ^^^ the trait `Foo` is not implemented for `f32`
|
= help: the trait `Foo` is implemented for `i32`
note: required by a bound in `Baz`

View File

@ -1,8 +1,8 @@
error[E0283]: type annotations needed: cannot satisfy `u32: C`
--> $DIR/coherence-overlap-trait-alias.rs:15:6
--> $DIR/coherence-overlap-trait-alias.rs:15:12
|
LL | impl C for u32 {}
| ^
| ^^^
|
note: multiple `impl`s satisfying `u32: C` found
--> $DIR/coherence-overlap-trait-alias.rs:14:1

View File

@ -16,10 +16,10 @@ LL | trait Foo<T: ?Sized> : Sized { fn take(self, x: &T) { } } // Note: T is siz
| ++++++++
error[E0277]: the size for values of type `[usize]` cannot be known at compilation time
--> $DIR/dst-sized-trait-param.rs:10:6
--> $DIR/dst-sized-trait-param.rs:10:21
|
LL | impl Foo<isize> for [usize] { }
| ^^^^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[usize]`
note: required by a bound in `Foo`

View File

@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/E0308-2.rs:9:6
--> $DIR/E0308-2.rs:9:13
|
LL | impl Eq for &dyn DynEq {}
| ^^ lifetime mismatch
| ^^^^^^^^^^ lifetime mismatch
|
= note: expected trait `<&dyn DynEq as PartialEq>`
found trait `<&(dyn DynEq + 'static) as PartialEq>`

View File

@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/issue-65230.rs:8:6
--> $DIR/issue-65230.rs:8:13
|
LL | impl T1 for &dyn T2 {}
| ^^ lifetime mismatch
| ^^^^^^^ lifetime mismatch
|
= note: expected trait `<&dyn T2 as T0>`
found trait `<&(dyn T2 + 'static) as T0>`

View File

@ -1,8 +1,8 @@
error[E0283]: type annotations needed: cannot satisfy `&(): Marker`
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:6:6
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:6:17
|
LL | impl Marker for &'_ () {}
| ^^^^^^
| ^^^^^^
|
note: multiple `impl`s satisfying `&(): Marker` found
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:6:1
@ -13,10 +13,10 @@ LL | impl Marker for &'_ () {}
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0283]: type annotations needed: cannot satisfy `&(): Marker`
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:7:6
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:7:17
|
LL | impl Marker for &'_ () {}
| ^^^^^^
| ^^^^^^
|
note: multiple `impl`s satisfying `&(): Marker` found
--> $DIR/overlap-marker-trait-with-underscore-lifetime.rs:6:1

View File

@ -1,8 +1,8 @@
error[E0283]: type annotations needed: cannot satisfy `(&'static (), &'a ()): A`
--> $DIR/region-overlap.rs:5:10
--> $DIR/region-overlap.rs:5:16
|
LL | impl<'a> A for (&'static (), &'a ()) {}
| ^
| ^^^^^^^^^^^^^^^^^^^^^
|
note: multiple `impl`s satisfying `(&'static (), &'a ()): A` found
--> $DIR/region-overlap.rs:5:1
@ -13,10 +13,10 @@ LL | impl<'a> A for (&'a (), &'static ()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0283]: type annotations needed: cannot satisfy `(&'a (), &'static ()): A`
--> $DIR/region-overlap.rs:6:10
--> $DIR/region-overlap.rs:6:16
|
LL | impl<'a> A for (&'a (), &'static ()) {}
| ^
| ^^^^^^^^^^^^^^^^^^^^^
|
note: multiple `impl`s satisfying `(&'a (), &'static ()): A` found
--> $DIR/region-overlap.rs:5:1

View File

@ -1,14 +1,14 @@
error[E0277]: the trait bound `S: ~const Foo` is not satisfied
--> $DIR/super-traits-fail.rs:15:12
--> $DIR/super-traits-fail.rs:15:20
|
LL | impl const Bar for S {}
| ^^^ the trait `~const Foo` is not implemented for `S`
| ^ the trait `~const Foo` is not implemented for `S`
|
note: the trait `Foo` is implemented for `S`, but that implementation is not `const`
--> $DIR/super-traits-fail.rs:15:12
--> $DIR/super-traits-fail.rs:15:20
|
LL | impl const Bar for S {}
| ^^^
| ^
note: required by a bound in `Bar`
--> $DIR/super-traits-fail.rs:8:12
|

View File

@ -1,8 +1,8 @@
error[E0277]: `MyError` doesn't implement `std::fmt::Display`
--> $DIR/issue-71363.rs:4:6
--> $DIR/issue-71363.rs:4:28
|
4 | impl std::error::Error for MyError {}
| ^^^^^^^^^^^^^^^^^ `MyError` cannot be formatted with the default formatter
| ^^^^^^^ `MyError` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `MyError`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
@ -10,10 +10,10 @@ note: required by a bound in `std::error::Error`
--> $SRC_DIR/core/src/error.rs:LL:COL
error[E0277]: `MyError` doesn't implement `Debug`
--> $DIR/issue-71363.rs:4:6
--> $DIR/issue-71363.rs:4:28
|
4 | impl std::error::Error for MyError {}
| ^^^^^^^^^^^^^^^^^ `MyError` cannot be formatted using `{:?}`
| ^^^^^^^ `MyError` cannot be formatted using `{:?}`
|
= help: the trait `Debug` is not implemented for `MyError`
= note: add `#[derive(Debug)]` to `MyError` or manually `impl Debug for MyError`

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `B: Clone` is not satisfied
--> $DIR/issue-79224.rs:18:17
--> $DIR/issue-79224.rs:18:29
|
LL | impl<B: ?Sized> Display for Cow<'_, B> {
| ^^^^^^^ the trait `Clone` is not implemented for `B`
| ^^^^^^^^^^ the trait `Clone` is not implemented for `B`
|
= note: required for `B` to implement `ToOwned`
help: consider further restricting this bound

View File

@ -1,8 +1,8 @@
error[E0277]: the size for values of type `B` cannot be known at compilation time
--> $DIR/unsized-bound.rs:2:12
--> $DIR/unsized-bound.rs:2:30
|
LL | impl<A, B> Trait<(A, B)> for (A, B) where A: ?Sized, B: ?Sized, {}
| - ^^^^^^^^^^^^^ doesn't have a size known at compile-time
| - ^^^^^^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -38,10 +38,10 @@ LL + impl<A, B> Trait<(A, B)> for (A, B) where B: ?Sized, {}
|
error[E0277]: the size for values of type `C` cannot be known at compilation time
--> $DIR/unsized-bound.rs:5:31
--> $DIR/unsized-bound.rs:5:52
|
LL | impl<A, B: ?Sized, C: ?Sized> Trait<(A, B, C)> for (A, B, C) where A: ?Sized, {}
| - ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| - ^^^^^^^^^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -92,10 +92,10 @@ LL + impl<A, B, C: ?Sized> Trait<(A, B, C)> for (A, B, C) where A: ?Sized, {}
|
error[E0277]: the size for values of type `B` cannot be known at compilation time
--> $DIR/unsized-bound.rs:10:28
--> $DIR/unsized-bound.rs:10:47
|
LL | impl<A: ?Sized, B: ?Sized> Trait2<(A, B)> for (A, B) {}
| - ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| - ^^^^^^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -131,10 +131,10 @@ LL + impl<A, B: ?Sized> Trait2<(A, B)> for (A, B) {}
|
error[E0277]: the size for values of type `A` cannot be known at compilation time
--> $DIR/unsized-bound.rs:14:9
--> $DIR/unsized-bound.rs:14:23
|
LL | impl<A> Trait3<A> for A where A: ?Sized {}
| - ^^^^^^^^^ doesn't have a size known at compile-time
| - ^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -154,10 +154,10 @@ LL | trait Trait3<A: ?Sized> {}
| ++++++++
error[E0277]: the size for values of type `A` cannot be known at compilation time
--> $DIR/unsized-bound.rs:17:17
--> $DIR/unsized-bound.rs:17:31
|
LL | impl<A: ?Sized> Trait4<A> for A {}
| - ^^^^^^^^^ doesn't have a size known at compile-time
| - ^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -177,10 +177,10 @@ LL | trait Trait4<A: ?Sized> {}
| ++++++++
error[E0277]: the size for values of type `X` cannot be known at compilation time
--> $DIR/unsized-bound.rs:20:12
--> $DIR/unsized-bound.rs:20:29
|
LL | impl<X, Y> Trait5<X, Y> for X where X: ?Sized {}
| - ^^^^^^^^^^^^ doesn't have a size known at compile-time
| - ^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
@ -200,10 +200,10 @@ LL | trait Trait5<A: ?Sized, B> {}
| ++++++++
error[E0277]: the size for values of type `X` cannot be known at compilation time
--> $DIR/unsized-bound.rs:23:20
--> $DIR/unsized-bound.rs:23:37
|
LL | impl<X: ?Sized, Y> Trait6<X, Y> for X {}
| - ^^^^^^^^^^^^ doesn't have a size known at compile-time
| - ^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `isize: Clone2` is not satisfied
--> $DIR/impl-bounds-checking.rs:10:6
--> $DIR/impl-bounds-checking.rs:10:24
|
LL | impl Getter<isize> for isize {
| ^^^^^^^^^^^^^ the trait `Clone2` is not implemented for `isize`
| ^^^^^ the trait `Clone2` is not implemented for `isize`
|
note: required by a bound in `Getter`
--> $DIR/impl-bounds-checking.rs:6:17

View File

@ -1,8 +1,8 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:13
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:28
|
LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
| ^^^^^^^^^^
| ^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:6
@ -15,10 +15,10 @@ note: ...but the lifetime must also be valid for the lifetime `'b` as defined he
LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
| ^^
note: ...so that the types are compatible
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:13
--> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:28
|
LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {
| ^^^^^^^^^^
| ^^^^^^^^^
= note: expected `T1<'a>`
found `T1<'_>`

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `T: Copy` is not satisfied
--> $DIR/issue-43784-supertrait.rs:8:9
--> $DIR/issue-43784-supertrait.rs:8:22
|
LL | impl<T> Complete for T {}
| ^^^^^^^^ the trait `Copy` is not implemented for `T`
| ^ the trait `Copy` is not implemented for `T`
|
note: required by a bound in `Complete`
--> $DIR/issue-43784-supertrait.rs:4:21

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `Foo: HasComponent<()>` is not satisfied
--> $DIR/issue-91594.rs:10:6
--> $DIR/issue-91594.rs:10:19
|
LL | impl HasComponent<<Foo as Component<Foo>>::Interface> for Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasComponent<()>` is not implemented for `Foo`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasComponent<()>` is not implemented for `Foo`
|
= help: the trait `HasComponent<<Foo as Component<Foo>>::Interface>` is implemented for `Foo`
note: required for `Foo` to implement `Component<Foo>`