Skip to content
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

Allow coercing functions whose signature differs in opaque types in their defining scope into a shared function pointer type #124297

Merged
merged 2 commits into from
May 23, 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
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
57 changes: 57 additions & 0 deletions tests/ui/fn/fn_def_opaque_coercion_to_fn_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Test that coercing between function items of different functions works,
//! as long as their signatures match. The resulting value is a function pointer.

#![feature(type_alias_impl_trait)]

fn foo<T>(t: T) -> T {
t
}

fn bar<T>(t: T) -> T {
t
}

type F = impl Sized;

fn f(a: F) {
let mut x = bar::<F>;
x = foo::<()>; //~ ERROR: mismatched types
x(a);
x(());
}

type I = impl Sized;

fn i(a: I) {
let mut x = bar::<()>;
x = foo::<I>; //~ ERROR: mismatched types
x(a);
x(());
}

type J = impl Sized;

fn j(a: J) {
let x = match true {
true => bar::<J>,
false => foo::<()>,
};
x(a);
x(());
}

fn k() -> impl Sized {
fn bind<T, F: FnOnce(T) -> T>(_: T, f: F) -> F {
f
}
let x = match true {
true => {
let f = foo;
bind(k(), f)
}
false => bar::<()>,
};
todo!()
}

fn main() {}
38 changes: 38 additions & 0 deletions tests/ui/fn/fn_def_opaque_coercion_to_fn_ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error[E0308]: mismatched types
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:18:9
|
LL | type F = impl Sized;
| ---------- the expected opaque type
...
LL | let mut x = bar::<F>;
| -------- expected due to this value
LL | x = foo::<()>;
| ^^^^^^^^^ expected fn item, found a different fn item
|
= note: expected fn item `fn(F) -> F {bar::<F>}`
found fn item `fn(()) {foo::<()>}`

error[E0308]: mismatched types
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:27:9
|
LL | fn foo<T>(t: T) -> T {
| -------------------- function `foo` defined here
...
LL | type I = impl Sized;
| ---------- the found opaque type
...
LL | let mut x = bar::<()>;
| --------- expected due to this value
LL | x = foo::<I>;
| ^^^^^^^^ expected fn item, found a different fn item
|
= note: expected fn item `fn(()) {bar::<()>}`
found fn item `fn(I) -> I {foo::<I>}`
help: use parentheses to call this function
|
LL | x = foo::<I>(/* I */);
| +++++++++

error: aborting due to 2 previous errors

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