don't skip coercions for types with errors

This commit is contained in:
Lukas Markeffsky 2024-02-10 23:11:01 +01:00
parent 6cc4843512
commit e330fe9c21
7 changed files with 30 additions and 70 deletions

View file

@ -186,17 +186,6 @@ fn coerce(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
let b = self.shallow_resolve(b);
debug!("Coerce.tys({:?} => {:?})", a, b);
// Just ignore error types.
if let Err(guar) = (a, b).error_reported() {
// Best-effort try to unify these types -- we're already on the error path,
// so this will have the side-effect of making sure we have no ambiguities
// due to `[type error]` and `_` not coercing together.
let _ = self.commit_if_ok(|_| {
self.at(&self.cause, self.param_env).eq(DefineOpaqueTypes::Yes, a, b)
});
return success(vec![], Ty::new_error(self.fcx.tcx, guar), vec![]);
}
// Coercing from `!` to any type is allowed:
if a.is_never() {
return success(simple(Adjust::NeverToAny)(b), b, vec![]);

View file

@ -0,0 +1,15 @@
// Regression test for an ICE: https://github.com/rust-lang/rust/issues/120884
// We still need to properly go through coercions between types with errors instead of
// shortcutting and returning success, because we need the adjustments for building the MIR.
pub fn has_error() -> TypeError {}
//~^ ERROR cannot find type `TypeError` in this scope
pub fn cast() -> *const u8 {
// Casting a function item to a data pointer in valid in HIR, but invalid in MIR.
// We need an adjustment (ReifyFnPointer) to insert a cast from the function item
// to a function pointer as a separate MIR statement.
has_error as *const u8
}
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0412]: cannot find type `TypeError` in this scope
--> $DIR/type-errors.rs:5:23
|
LL | pub fn has_error() -> TypeError {}
| ^^^^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0412`.

View file

@ -9,8 +9,6 @@ fn baz<U,
(y: T) { //~ ERROR E0401
}
bfnr(x);
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}

View file

@ -21,7 +21,7 @@ LL | (y: T) {
| ^ use of generic parameter from outer item
error[E0401]: can't use `Self` from outer item
--> $DIR/E0401.rs:24:25
--> $DIR/E0401.rs:22:25
|
LL | impl<T> Iterator for A<T> {
| ---- `Self` type implicitly declared here, by this `impl`
@ -32,45 +32,6 @@ LL | fn helper(sel: &Self) -> u8 {
| use of `Self` from outer item
| refer to the type directly here instead
error[E0283]: type annotations needed
--> $DIR/E0401.rs:11:5
|
LL | bfnr(x);
| ^^^^ cannot infer type of the type parameter `V` declared on the function `bfnr`
|
= note: cannot satisfy `_: Baz<_>`
note: required by a bound in `bfnr`
--> $DIR/E0401.rs:4:19
|
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
| ^^^^^^ required by this bound in `bfnr`
help: consider specifying the generic arguments
|
LL | bfnr::<U, V, W>(x);
| +++++++++++
error: aborting due to 3 previous errors
error[E0283]: type annotations needed
--> $DIR/E0401.rs:11:5
|
LL | bfnr(x);
| ^^^^ cannot infer type of the type parameter `W` declared on the function `bfnr`
|
= note: multiple `impl`s satisfying `_: Fn()` found in the following crates: `alloc`, `core`:
- impl<A, F> Fn<A> for &F
where A: Tuple, F: Fn<A>, F: ?Sized;
- impl<Args, F, A> Fn<Args> for Box<F, A>
where Args: Tuple, F: Fn<Args>, A: Allocator, F: ?Sized;
note: required by a bound in `bfnr`
--> $DIR/E0401.rs:4:30
|
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
| ^^^^ required by this bound in `bfnr`
help: consider specifying the generic arguments
|
LL | bfnr::<U, V, W>(x);
| +++++++++++
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0283, E0401.
For more information about an error, try `rustc --explain E0283`.
For more information about this error, try `rustc --explain E0401`.

View file

@ -6,7 +6,7 @@
unsafe fn test() {
let j = W(());
let pointer = &j as *const _; //~ ERROR type annotations needed
let pointer = &j as *const _;
core::arch::asm!(
"nop",
in("eax") pointer,

View file

@ -4,18 +4,6 @@ error[E0412]: cannot find type `Oops` in this scope
LL | struct W<T: ?Sized>(Oops);
| ^^^^ not found in this scope
error[E0282]: type annotations needed for `*const W<T>`
--> $DIR/issue-104510-ice.rs:9:9
|
LL | let pointer = &j as *const _;
| ^^^^^^^
|
help: consider giving `pointer` an explicit type, where the type for type parameter `T` is specified
|
LL | let pointer: *const W<T> = &j as *const _;
| +++++++++++++
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0282, E0412.
For more information about an error, try `rustc --explain E0282`.
For more information about this error, try `rustc --explain E0412`.