Skip to content

eagerly prove WF when resolving fully qualified paths #136928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,13 +798,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`")
}
};

self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
);
self.select_obligations_where_possible(|_| {});

if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id)
{
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
);
// Return directly on cache hit. This is useful to avoid doubly reporting
// errors with default match binding modes. See #44614.
let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
Expand All @@ -824,18 +827,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let trait_missing_method =
matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
// If we have a path like `MyTrait::missing_method`, then don't register
// a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise,
// register a WF obligation so that we can detect any additional
// errors in the self type.
if !trait_missing_method {
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
);
}

if item_name.name != kw::Empty {
self.report_method_error(
hir_id,
Expand All @@ -849,14 +840,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
result
});

if result.is_ok() {
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
);
}

// Write back the new resolution.
self.write_resolution(hir_id, result);
(
Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-consts/associated-const-in-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ impl dyn Trait {
//~^ ERROR the trait `Trait` is not dyn compatible [E0038]
const fn n() -> usize { Self::N }
//~^ ERROR the trait `Trait` is not dyn compatible [E0038]
//~| ERROR the trait `Trait` is not dyn compatible
}

fn main() {}
18 changes: 17 additions & 1 deletion tests/ui/associated-consts/associated-const-in-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ LL | const N: usize;
| ^ ...because it contains this associated `const`
= help: consider moving `N` to another trait

error: aborting due to 2 previous errors
error[E0038]: the trait `Trait` is not dyn compatible
--> $DIR/associated-const-in-trait.rs:9:29
|
LL | const fn n() -> usize { Self::N }
| ^^^^^^^ `Trait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
--> $DIR/associated-const-in-trait.rs:4:11
|
LL | trait Trait {
| ----- this trait is not dyn compatible...
LL | const N: usize;
| ^ ...because it contains this associated `const`
= help: consider moving `N` to another trait

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0038`.
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
error[E0599]: no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:23
|
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
| ---------------------------------- associated item `C` not found for this struct
...
LL | Fail::<i32, u32>::C
| ^ associated item not found in `Fail<i32, u32>`
|
= note: the associated item was found for
- `Fail<i32, i32>`

error[E0271]: type mismatch resolving `<i32 as Proj>::Assoc == u32`
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:5
|
Expand All @@ -27,6 +15,18 @@ note: required by a bound in `Fail`
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
| ^^^^^^^^^ required by this bound in `Fail`

error[E0599]: no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:23
|
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
| ---------------------------------- associated item `C` not found for this struct
...
LL | Fail::<i32, u32>::C
| ^ associated item not found in `Fail<i32, u32>`
|
= note: the associated item was found for
- `Fail<i32, i32>`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0271, E0599.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn f<'a, T: X<'a> + ?Sized>(x: &<T as X<'a>>::U) {
<<T as X<'_>>::U>::clone(x);
//~^ ERROR the trait bound `for<'b> <T as X<'b>>::U: Clone` is not satisfied
//~| ERROR the trait bound `for<'b> <T as X<'b>>::U: Clone` is not satisfied
//~| ERROR the trait bound `for<'b> <T as X<'b>>::U: Clone` is not satisfied
//~| ERROR the trait bound `<T as X<'_>>::U: Clone` is not satisfied
}

Expand Down
21 changes: 20 additions & 1 deletion tests/ui/associated-types/hr-associated-type-bound-object.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ help: consider further restricting the associated type
LL | fn f<'a, T: X<'a> + ?Sized>(x: &<T as X<'a>>::U) where <T as X<'_>>::U: Clone {
| ++++++++++++++++++++++++++++

error[E0277]: the trait bound `for<'b> <T as X<'b>>::U: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-object.rs:9:5
|
LL | <<T as X<'_>>::U>::clone(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'b> Clone` is not implemented for `<T as X<'b>>::U`
|
note: required by a bound in `X`
--> $DIR/hr-associated-type-bound-object.rs:3:33
|
LL | trait X<'a>
| - required by a bound in this trait
LL | where
LL | for<'b> <Self as X<'b>>::U: Clone,
| ^^^^^ required by this bound in `X`
help: consider further restricting the associated type
|
LL | fn f<'a, T: X<'a> + ?Sized>(x: &<T as X<'a>>::U) where for<'b> <T as X<'b>>::U: Clone {
| ++++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `for<'b> <T as X<'b>>::U: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-object.rs:9:5
|
Expand All @@ -66,6 +85,6 @@ help: consider further restricting the associated type
LL | fn f<'a, T: X<'a> + ?Sized>(x: &<T as X<'a>>::U) where for<'b> <T as X<'b>>::U: Clone {
| ++++++++++++++++++++++++++++++++++++

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ where
<T::W>::clone(x);
//~^ the trait bound `str: Clone` is not satisfied
//~| the trait bound `str: Clone` is not satisfied
//~| the trait bound `str: Clone` is not satisfied
}
}

Expand Down
22 changes: 19 additions & 3 deletions tests/ui/associated-types/hr-associated-type-bound-param-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | for<'b> <T as Z<'b, u16>>::W: Clone,
| ^^^^^ required by this bound in `Z`

error[E0277]: the trait bound `str: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-param-2.rs:17:14
--> $DIR/hr-associated-type-bound-param-2.rs:18:14
|
LL | type W = str;
| ^^^ the trait `Clone` is not implemented for `str`
Expand Down Expand Up @@ -63,6 +63,22 @@ LL | {
LL | type W: ?Sized;
| - required by a bound in this associated type

error[E0277]: the trait bound `str: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-param-2.rs:10:9
|
LL | <T::W>::clone(x);
| ^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str`
|
= help: the trait `Clone` is implemented for `String`
note: required by a bound in `Z`
--> $DIR/hr-associated-type-bound-param-2.rs:6:35
|
LL | trait Z<'a, T: ?Sized>
| - required by a bound in this trait
...
LL | for<'b> <T as Z<'b, u16>>::W: Clone,
| ^^^^^ required by this bound in `Z`

error[E0277]: the trait bound `str: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-param-2.rs:10:9
|
Expand All @@ -80,7 +96,7 @@ LL | for<'b> <T as Z<'b, u16>>::W: Clone,
| ^^^^^ required by this bound in `Z`

error[E0277]: the trait bound `str: Clone` is not satisfied
--> $DIR/hr-associated-type-bound-param-2.rs:22:10
--> $DIR/hr-associated-type-bound-param-2.rs:23:10
|
LL | 1u16.h("abc");
| ^ the trait `Clone` is not implemented for `str`
Expand All @@ -95,6 +111,6 @@ LL | for<'b> <T as Z<'b, u16>>::W: Clone,
LL | fn h(&self, x: &T::W) {
| - required by a bound in this associated function

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0277`.
24 changes: 12 additions & 12 deletions tests/ui/const-generics/defaults/doesnt_infer.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ error[E0284]: type annotations needed for `Foo<_>`
--> $DIR/doesnt_infer.rs:13:9
|
LL | let foo = Foo::foo();
| ^^^ ---------- type must be known at this point
| ^^^ --- type must be known at this point
|
note: required by a const generic parameter in `Foo::<N>::foo`
--> $DIR/doesnt_infer.rs:5:6
note: required by a const generic parameter in `Foo`
--> $DIR/doesnt_infer.rs:3:12
|
LL | impl<const N: u32> Foo<N> {
| ^^^^^^^^^^^^ required by this const generic parameter in `Foo::<N>::foo`
LL | fn foo() -> Self {
| --- required by a bound in this associated function
LL | struct Foo<const N: u32 = 2>;
| ^^^^^^^^^^^^^^^^ required by this const generic parameter in `Foo`
help: consider giving `foo` an explicit type, where the value of const parameter `N` is specified
|
LL | let foo: Foo<N> = Foo::foo();
Expand All @@ -20,13 +18,15 @@ error[E0284]: type annotations needed for `Foo<_>`
--> $DIR/doesnt_infer.rs:13:9
|
LL | let foo = Foo::foo();
| ^^^ --- type must be known at this point
| ^^^ ---------- type must be known at this point
|
note: required by a const generic parameter in `Foo`
--> $DIR/doesnt_infer.rs:3:12
note: required by a const generic parameter in `Foo::<N>::foo`
--> $DIR/doesnt_infer.rs:5:6
|
LL | struct Foo<const N: u32 = 2>;
| ^^^^^^^^^^^^^^^^ required by this const generic parameter in `Foo`
LL | impl<const N: u32> Foo<N> {
| ^^^^^^^^^^^^ required by this const generic parameter in `Foo::<N>::foo`
LL | fn foo() -> Self {
| --- required by a bound in this associated function
help: consider giving `foo` an explicit type, where the value of const parameter `N` is specified
|
LL | let foo: Foo<N> = Foo::foo();
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/const-generics/generic_arg_infer/issue-91614.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0284]: type annotations needed for `Mask<_, _>`
--> $DIR/issue-91614.rs:6:9
|
LL | let y = Mask::<_, _>::splat(false);
| ^ -------------------------- type must be known at this point
| ^ ------------ type must be known at this point
|
note: required by a const generic parameter in `Mask::<T, N>::splat`
note: required by a const generic parameter in `Mask`
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
|
Expand All @@ -15,9 +15,9 @@ error[E0284]: type annotations needed for `Mask<_, _>`
--> $DIR/issue-91614.rs:6:9
|
LL | let y = Mask::<_, _>::splat(false);
| ^ ------------ type must be known at this point
| ^ -------------------------- type must be known at this point
|
note: required by a const generic parameter in `Mask`
note: required by a const generic parameter in `Mask::<T, N>::splat`
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
|
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/const-generics/generic_const_exprs/issue-62504.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ------------------ type must be known at this point
| ^^^^^^^^^ ----------- type must be known at this point
|
note: required by a const generic parameter in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
note: required by a const generic parameter in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
|
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder`
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
Expand All @@ -40,13 +38,15 @@ error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ----------- type must be known at this point
| ^^^^^^^^^ ------------------ type must be known at this point
|
note: required by a const generic parameter in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
note: required by a const generic parameter in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
|
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder`
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/const-generics/generic_const_exprs/issue-62504.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ------------------ type must be known at this point
| ^^^^^^^^^ ----------- type must be known at this point
|
note: required by a const generic parameter in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
note: required by a const generic parameter in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
|
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder`
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
Expand All @@ -42,13 +40,15 @@ error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ----------- type must be known at this point
| ^^^^^^^^^ ------------------ type must be known at this point
|
note: required by a const generic parameter in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
note: required by a const generic parameter in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
|
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder`
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: internal compiler error: compiler/rustc_const_eval/src/interpret/operator

Box<dyn Any>
query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at $DIR/issue-80742.rs:26:1: 28:32>::{constant#0}`
#0 [eval_to_allocation_raw] const-evaluating + checking `Inline::{constant#0}`
#1 [eval_to_valtree] evaluating type-level constant
... and 2 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: aborting due to 1 previous error
Expand Down
Loading
Loading