Skip to content

Commit

Permalink
Allow coercing functions whose signature differs in opaque types in t…
Browse files Browse the repository at this point in the history
…heir defining scope into a shared function pointer type
  • Loading branch information
oli-obk committed Apr 23, 2024
1 parent a18f043 commit c24148e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 43 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let sig = self
.at(cause, self.param_env)
.trace(prev_ty, new_ty)
.lub(DefineOpaqueTypes::No, a_sig, b_sig)
.lub(DefineOpaqueTypes::Yes, a_sig, b_sig)
.map(|ok| self.register_infer_ok_obligations(ok))?;

// Reify both sides and return the reified fn pointer type.
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/fn/fn_def_opaque_coercion_to_fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type J = impl Sized;
fn j(a: J) {
let x = match true {
true => bar::<J>,
false => foo::<()>, //~ ERROR: incompatible types
false => foo::<()>,
};
x(a);
x(());
Expand All @@ -49,7 +49,7 @@ fn k() -> impl Sized {
let f = foo;
bind(k(), f)
}
false => bar::<()>, //~ ERROR: incompatible types
false => bar::<()>,
};
todo!()
}
Expand Down
41 changes: 1 addition & 40 deletions tests/ui/fn/fn_def_opaque_coercion_to_fn_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,6 @@ help: use parentheses to call this function
LL | x = foo::<I>(/* I */);
| +++++++++

error[E0308]: `match` arms have incompatible types
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:37:18
|
LL | type J = impl Sized;
| ---------- the expected opaque type
...
LL | let x = match true {
| _____________-
LL | | true => bar::<J>,
| | -------- this is found to be of type `fn(J) -> J {bar::<J>}`
LL | | false => foo::<()>,
| | ^^^^^^^^^ expected opaque type, found `()`
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected fn item `fn(J) -> J {bar::<J>}`
found fn item `fn(()) {foo::<()>}`

error[E0308]: `match` arms have incompatible types
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:52:18
|
LL | fn k() -> impl Sized {
| ---------- the expected opaque type
...
LL | let x = match true {
| _____________-
LL | | true => {
LL | | let f = foo;
LL | | bind(k(), f)
| | ------------ this is found to be of type `fn(impl Sized) -> impl Sized {foo::<impl Sized>}`
LL | | }
LL | | false => bar::<()>,
| | ^^^^^^^^^ expected opaque type, found `()`
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected fn item `fn(impl Sized) -> impl Sized {foo::<impl Sized>}`
found fn item `fn(()) {bar::<()>}`

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

For more information about this error, try `rustc --explain E0308`.

0 comments on commit c24148e

Please sign in to comment.