Skip to content

Commit 2af0b16

Browse files
authored
Rollup merge of #70982 - ldm0:fncoerce, r=eddyb
Normalize function signature in function casting check procedure Fixes #54094 ```rust trait Zoo { type X; } impl Zoo for u16 { type X = usize; } fn foo(abc: <u16 as Zoo>::X) {} fn main() { let x: *const u8 = foo as _; } ``` Currently a `FnDef` need to be checked if it's able to cast to `FnPtr` before it is actually casted. But the signature of `FnPtr` target's associated types are not normalized: https://github.com/rust-lang/rust/blob/96d77f0e5f103612d62b85938aacfb33f5768433/src/librustc_typeck/check/cast.rs#L536-L553 However, during the coercion check, the signature of `FnPtr` target's associated types are normalized (The `<u16 as Zoo>::X` turns into `usize`). https://github.com/rust-lang/rust/blob/96d77f0e5f103612d62b85938aacfb33f5768433/src/librustc_typeck/check/coercion.rs#L687-L729 This inconsistency leads to the error:`Err(Sorts(ExpectedFound { expected: <u16 as Zoo>::X, found: usize }))`.
2 parents 12e3c7b + 75cc403 commit 2af0b16

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/librustc_typeck/check/cast.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
536536
match self.expr_ty.kind {
537537
ty::FnDef(..) => {
538538
// Attempt a coercion to a fn pointer type.
539-
let f = self.expr_ty.fn_sig(fcx.tcx);
539+
let f = fcx.normalize_associated_types_in(
540+
self.expr.span,
541+
&self.expr_ty.fn_sig(fcx.tcx),
542+
);
540543
let res = fcx.try_coerce(
541544
self.expr,
542545
self.expr_ty,

src/test/ui/issues/issue-54094.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
trait Zoo {
3+
type X;
4+
}
5+
6+
impl Zoo for u16 {
7+
type X = usize;
8+
}
9+
10+
fn foo(abc: <u16 as Zoo>::X) {}
11+
12+
fn main() {
13+
let x: *const u8 = foo as _;
14+
}

0 commit comments

Comments
 (0)