Skip to content

don't skip coercions for types with errors #120895

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 1 commit into from
Feb 11, 2024
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
11 changes: 0 additions & 11 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,6 @@ impl<'f, 'tcx> Coerce<'f, '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![]);
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/coercion/type-errors.rs
Original file line number Diff line number Diff line change
@@ -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() {}
9 changes: 9 additions & 0 deletions tests/ui/coercion/type-errors.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 0 additions & 2 deletions tests/ui/error-codes/E0401.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ fn foo<T>(x: T) {
(y: T) { //~ ERROR E0401
}
bfnr(x);
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}


Expand Down
45 changes: 3 additions & 42 deletions tests/ui/error-codes/E0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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[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
error: aborting due to 3 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`.
2 changes: 1 addition & 1 deletion tests/ui/typeck/issue-104510-ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct W<T: ?Sized>(Oops);

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,
Expand Down
16 changes: 2 additions & 14 deletions tests/ui/typeck/issue-104510-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 previous errors
error: aborting due to 1 previous error

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`.