forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#129168 - BoxyUwU:mismatched_ty_correct_id, r=compiler-errors Return correct HirId when finding body owner in diagnostics Fixes rust-lang#129145 Fixes rust-lang#128810 r? ```@compiler-errors``` ```rust fn generic<const N: u32>() {} trait Collate<const A: u32> { type Pass; fn collate(self) -> Self::Pass; } impl<const B: u32> Collate<B> for i32 { type Pass = (); fn collate(self) -> Self::Pass { generic::<{ true }>() //~^ ERROR: mismatched types } } ``` When type checking the `{ true }` anon const we would error with a type mismatch. This then results in diagnostics code attempting to check whether its due to a type mismatch with the return type. That logic was implemented by walking up the hir until we reached the body owner, except instead of using the `enclosing_body_owner` function it special cased various hir nodes incorrectly resulting in us walking out of the anon const and stopping at `fn collate` instead. This then resulted in diagnostics logic inside of the anon consts `ParamEnv` attempting to do trait solving involving the `<i32 as Collate<B>>::Pass` type which ICEs because it is in the wrong environment. I have rewritten this function to just walk up until it hits the `enclosing_body_owner` and made some other changes since I found this pretty hard to read/understand. Hopefully it's easier to understand now, it also makes it more obvious that this is not implemented in a very principled way and is definitely missing cases :)
- Loading branch information
Showing
6 changed files
with
199 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
tests/ui/delegation/correct_body_owner_parent_found_in_diagnostics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![feature(fn_delegation)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::marker::PhantomData; | ||
|
||
pub struct InvariantRef<'a, T: ?Sized>(&'a T, PhantomData<&'a mut &'a T>); | ||
|
||
impl<'a> InvariantRef<'a, ()> { | ||
pub const NEW: Self = InvariantRef::new(&()); | ||
//~^ ERROR: no function or associated item named `new` found | ||
} | ||
|
||
trait Trait { | ||
fn foo(&self) -> u8 { 0 } | ||
fn bar(&self) -> u8 { 1 } | ||
fn meh(&self) -> u8 { 2 } | ||
} | ||
|
||
struct Z(u8); | ||
|
||
impl Trait for Z { | ||
reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
//~^ ERROR: use of undeclared lifetime name `'a` | ||
//~| ERROR: use of undeclared lifetime name `'a` | ||
//~| ERROR: use of undeclared lifetime name `'a` | ||
//~| ERROR: the trait bound `u8: Trait` is not satisfied | ||
//~| ERROR: the trait bound `u8: Trait` is not satisfied | ||
//~| ERROR: the trait bound `u8: Trait` is not satisfied | ||
//~| ERROR: mismatched types | ||
//~| ERROR: mismatched types | ||
//~| ERROR: mismatched types | ||
} | ||
|
||
fn main() { } |
113 changes: 113 additions & 0 deletions
113
tests/ui/delegation/correct_body_owner_parent_found_in_diagnostics.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
error[E0261]: use of undeclared lifetime name `'a` | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:68 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | reuse <u8 as Trait>::{foo'a, , bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| +++ | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | impl<'a> Trait for Z { | ||
| ++++ | ||
|
||
error[E0261]: use of undeclared lifetime name `'a` | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:68 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar'a, , meh} { &const { InvariantRef::<'a>::NEW } } | ||
| +++ | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | impl<'a> Trait for Z { | ||
| ++++ | ||
|
||
error[E0261]: use of undeclared lifetime name `'a` | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:68 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh'a, } { &const { InvariantRef::<'a>::NEW } } | ||
| +++ | ||
help: consider introducing lifetime `'a` here | ||
| | ||
LL | impl<'a> Trait for Z { | ||
| ++++ | ||
|
||
error[E0599]: no function or associated item named `new` found for struct `InvariantRef` in the current scope | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:9:41 | ||
| | ||
LL | pub struct InvariantRef<'a, T: ?Sized>(&'a T, PhantomData<&'a mut &'a T>); | ||
| -------------------------------------- function or associated item `new` not found for this struct | ||
... | ||
LL | pub const NEW: Self = InvariantRef::new(&()); | ||
| ^^^ function or associated item not found in `InvariantRef<'_, _>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:53 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `InvariantRef<'_, ()>` | ||
| | ||
= note: expected type `u8` | ||
found struct `InvariantRef<'_, ()>` | ||
|
||
error[E0277]: the trait bound `u8: Trait` is not satisfied | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:12 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ the trait `Trait` is not implemented for `u8` | ||
| | ||
= help: the trait `Trait` is implemented for `Z` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:53 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `InvariantRef<'_, ()>` | ||
| | ||
= note: expected type `u8` | ||
found struct `InvariantRef<'_, ()>` | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error[E0277]: the trait bound `u8: Trait` is not satisfied | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:12 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ the trait `Trait` is not implemented for `u8` | ||
| | ||
= help: the trait `Trait` is implemented for `Z` | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:53 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `InvariantRef<'_, ()>` | ||
| | ||
= note: expected type `u8` | ||
found struct `InvariantRef<'_, ()>` | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error[E0277]: the trait bound `u8: Trait` is not satisfied | ||
--> $DIR/correct_body_owner_parent_found_in_diagnostics.rs:22:12 | ||
| | ||
LL | reuse <u8 as Trait>::{foo, bar, meh} { &const { InvariantRef::<'a>::NEW } } | ||
| ^^ the trait `Trait` is not implemented for `u8` | ||
| | ||
= help: the trait `Trait` is implemented for `Z` | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error: aborting due to 10 previous errors | ||
|
||
Some errors have detailed explanations: E0261, E0277, E0308, E0599. | ||
For more information about an error, try `rustc --explain E0261`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
fn generic<const N: u32>() {} | ||
|
||
trait Collate<const A: u32> { | ||
type Pass; | ||
fn collate(self) -> Self::Pass; | ||
} | ||
|
||
impl<const B: u32> Collate<B> for i32 { | ||
type Pass = (); | ||
fn collate(self) -> Self::Pass { | ||
generic::<{ true }>() | ||
//~^ ERROR: mismatched types | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/const-in-fn-call-generics.rs:11:21 | ||
| | ||
LL | generic::<{ true }>() | ||
| ^^^^ expected `u32`, found `bool` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |