Skip to content

Commit c3f9c4f

Browse files
Use equality when relating formal and expected type in arg checking
1 parent d571ae8 commit c3f9c4f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
292292

293293
let coerce_error =
294294
self.coerce(provided_arg, checked_ty, coerced_ty, AllowTwoPhase::Yes, None).err();
295-
296295
if coerce_error.is_some() {
297296
return Compatibility::Incompatible(coerce_error);
298297
}
299298

300-
// 3. Check if the formal type is a supertype of the checked one
301-
// and register any such obligations for future type checks
302-
let supertype_error = self.at(&self.misc(provided_arg.span), self.param_env).sup(
299+
// 3. Check if the formal type is actually equal to the checked one
300+
// and register any such obligations for future type checks.
301+
let formal_ty_error = self.at(&self.misc(provided_arg.span), self.param_env).eq(
303302
DefineOpaqueTypes::Yes,
304303
formal_input_ty,
305304
coerced_ty,
306305
);
307306

308307
// If neither check failed, the types are compatible
309-
match supertype_error {
308+
match formal_ty_error {
310309
Ok(InferOk { obligations, value: () }) => {
311310
self.register_predicates(obligations);
312311
Compatibility::Compatible

Diff for: tests/ui/coercion/constrain-expectation-in-arg.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
trait Trait {
4+
type Item;
5+
}
6+
7+
struct Struct<A: Trait<Item = B>, B> {
8+
pub field: A,
9+
}
10+
11+
fn identity<T>(x: T) -> T {
12+
x
13+
}
14+
15+
fn test<A: Trait<Item = B>, B>(x: &Struct<A, B>) {
16+
let x: &Struct<_, _> = identity(x);
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)