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 31, 2024
1 parent d571ae8 commit c3f9c4f
Show file tree
Hide file tree
Showing 2 changed files with 23 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 @@ -292,21 +292,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
19 changes: 19 additions & 0 deletions tests/ui/coercion/constrain-expectation-in-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ check-pass

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);
}

fn main() {}

0 comments on commit c3f9c4f

Please sign in to comment.