Skip to content

Commit

Permalink
Use equality when relating formal and expected type in arg checking
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Aug 20, 2024
1 parent 636d7ff commit aca15e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let coerce_error =
self.coerce(provided_arg, checked_ty, coerced_ty, AllowTwoPhase::Yes, None).err();

if coerce_error.is_some() {
return Compatibility::Incompatible(coerce_error);
}

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

// If neither check failed, the types are compatible
match supertype_error {
match formal_ty_error {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
Compatibility::Compatible
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/coercion/constrain-expectation-in-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait Trait {
type Item;
}

struct Struct<A: Trait<Item = B>, B> {
pub field: A,
}

fn identity<T>(x: T) -> T {
x
}

fn test<A: Trait<Item = B>, B>(x: &Struct<A, B>) {
let x: &Struct<_, _> = identity(x);
}

0 comments on commit aca15e4

Please sign in to comment.