Skip to content

Commit 1d0702e

Browse files
Use equality when relating formal and expected type in arg checking
1 parent 636d7ff commit 1d0702e

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
@@ -291,21 +291,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
291291

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

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

307306
// If neither check failed, the types are compatible
308-
match supertype_error {
307+
match formal_ty_error {
309308
Ok(InferOk { obligations, value: () }) => {
310309
self.register_predicates(obligations);
311310
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)