Skip to content

Commit 32b5635

Browse files
Merge #9656
9656: Revert #9655 because it breaks some type inference r=flodiebold a=flodiebold This reverts commit 8c8c6fb, reversing changes made to ec7b4cb. bors r+ Co-authored-by: Florian Diebold <[email protected]>
2 parents 8c8c6fb + 7c00ca2 commit 32b5635

File tree

4 files changed

+15
-142
lines changed

4 files changed

+15
-142
lines changed

crates/hir_ty/src/infer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,8 @@ impl Expectation {
845845
/// which still is useful, because it informs integer literals and the like.
846846
/// See the test case `test/ui/coerce-expect-unsized.rs` and #20169
847847
/// for examples of where this comes up,.
848-
fn rvalue_hint(table: &mut unify::InferenceTable, ty: Ty) -> Self {
849-
// FIXME: do struct_tail_without_normalization
850-
match table.resolve_ty_shallow(&ty).kind(&Interner) {
848+
fn rvalue_hint(ty: Ty) -> Self {
849+
match ty.strip_references().kind(&Interner) {
851850
TyKind::Slice(_) | TyKind::Str | TyKind::Dyn(_) => Expectation::RValueLikeUnsized(ty),
852851
_ => Expectation::has_type(ty),
853852
}

crates/hir_ty/src/infer/expr.rs

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -340,25 +340,11 @@ impl<'a> InferenceContext<'a> {
340340
None => (Vec::new(), self.err_ty()),
341341
};
342342
self.register_obligations_for_call(&callee_ty);
343-
344-
let expected_inputs = self.expected_inputs_for_expected_output(
345-
expected,
346-
ret_ty.clone(),
347-
param_tys.clone(),
348-
);
349-
350-
self.check_call_arguments(args, &expected_inputs, &param_tys);
343+
self.check_call_arguments(args, &param_tys);
351344
self.normalize_associated_types_in(ret_ty)
352345
}
353346
Expr::MethodCall { receiver, args, method_name, generic_args } => self
354-
.infer_method_call(
355-
tgt_expr,
356-
*receiver,
357-
args,
358-
method_name,
359-
generic_args.as_deref(),
360-
expected,
361-
),
347+
.infer_method_call(tgt_expr, *receiver, args, method_name, generic_args.as_deref()),
362348
Expr::Match { expr, arms } => {
363349
let input_ty = self.infer_expr(*expr, &Expectation::none());
364350

@@ -589,7 +575,7 @@ impl<'a> InferenceContext<'a> {
589575
// FIXME: record type error - expected reference but found ptr,
590576
// which cannot be coerced
591577
}
592-
Expectation::rvalue_hint(&mut self.table, Ty::clone(exp_inner))
578+
Expectation::rvalue_hint(Ty::clone(exp_inner))
593579
} else {
594580
Expectation::none()
595581
};
@@ -916,7 +902,6 @@ impl<'a> InferenceContext<'a> {
916902
args: &[ExprId],
917903
method_name: &Name,
918904
generic_args: Option<&GenericArgs>,
919-
expected: &Expectation,
920905
) -> Ty {
921906
let receiver_ty = self.infer_expr(receiver, &Expectation::none());
922907
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
@@ -950,7 +935,7 @@ impl<'a> InferenceContext<'a> {
950935
};
951936
let method_ty = method_ty.substitute(&Interner, &substs);
952937
self.register_obligations_for_call(&method_ty);
953-
let (formal_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
938+
let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
954939
Some(sig) => {
955940
if !sig.params().is_empty() {
956941
(sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
@@ -960,87 +945,28 @@ impl<'a> InferenceContext<'a> {
960945
}
961946
None => (self.err_ty(), Vec::new(), self.err_ty()),
962947
};
963-
self.unify(&formal_receiver_ty, &receiver_ty);
964-
965-
let expected_inputs =
966-
self.expected_inputs_for_expected_output(expected, ret_ty.clone(), param_tys.clone());
948+
self.unify(&expected_receiver_ty, &receiver_ty);
967949

968-
self.check_call_arguments(args, &expected_inputs, &param_tys);
950+
self.check_call_arguments(args, &param_tys);
969951
self.normalize_associated_types_in(ret_ty)
970952
}
971953

972-
fn expected_inputs_for_expected_output(
973-
&mut self,
974-
expected_output: &Expectation,
975-
output: Ty,
976-
inputs: Vec<Ty>,
977-
) -> Vec<Ty> {
978-
// rustc does a snapshot here and rolls back the unification, but since
979-
// we actually want to keep unbound variables in the result it then
980-
// needs to do 'fudging' to recreate them. So I'm not sure rustc's
981-
// approach is cleaner than ours, which is to create independent copies
982-
// of the variables before unifying. It might be more performant though,
983-
// so we might want to benchmark when we can actually do
984-
// snapshot/rollback.
985-
if let Some(expected_ty) = expected_output.to_option(&mut self.table) {
986-
let (expected_ret_ty, expected_params) = self.table.reinstantiate((output, inputs));
987-
if self.table.try_unify(&expected_ty, &expected_ret_ty).is_ok() {
988-
expected_params
989-
} else {
990-
Vec::new()
991-
}
992-
} else {
993-
Vec::new()
994-
}
995-
}
996-
997-
fn check_call_arguments(&mut self, args: &[ExprId], expected_inputs: &[Ty], param_tys: &[Ty]) {
954+
fn check_call_arguments(&mut self, args: &[ExprId], param_tys: &[Ty]) {
998955
// Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
999956
// We do this in a pretty awful way: first we type-check any arguments
1000957
// that are not closures, then we type-check the closures. This is so
1001958
// that we have more information about the types of arguments when we
1002959
// type-check the functions. This isn't really the right way to do this.
1003960
for &check_closures in &[false, true] {
1004961
let param_iter = param_tys.iter().cloned().chain(repeat(self.err_ty()));
1005-
let expected_iter = expected_inputs
1006-
.iter()
1007-
.cloned()
1008-
.chain(param_iter.clone().skip(expected_inputs.len()));
1009-
for ((&arg, param_ty), expected_ty) in args.iter().zip(param_iter).zip(expected_iter) {
962+
for (&arg, param_ty) in args.iter().zip(param_iter) {
1010963
let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
1011964
if is_closure != check_closures {
1012965
continue;
1013966
}
1014967

1015-
// the difference between param_ty and expected here is that
1016-
// expected is the parameter when the expected *return* type is
1017-
// taken into account. So in `let _: &[i32] = identity(&[1, 2])`
1018-
// the expected type is already `&[i32]`, whereas param_ty is
1019-
// still an unbound type variable. We don't always want to force
1020-
// the parameter to coerce to the expected type (for example in
1021-
// `coerce_unsize_expected_type_4`).
1022968
let param_ty = self.normalize_associated_types_in(param_ty);
1023-
let expected = Expectation::rvalue_hint(&mut self.table, expected_ty);
1024-
// infer with the expected type we have...
1025-
let ty = self.infer_expr_inner(arg, &expected);
1026-
1027-
// then coerce to either the expected type or just the formal parameter type
1028-
let coercion_target = if let Some(ty) = expected.only_has_type(&mut self.table) {
1029-
// if we are coercing to the expectation, unify with the
1030-
// formal parameter type to connect everything
1031-
self.unify(&ty, &param_ty);
1032-
ty
1033-
} else {
1034-
param_ty
1035-
};
1036-
if !coercion_target.is_unknown() {
1037-
if self.coerce(Some(arg), &ty, &coercion_target).is_err() {
1038-
self.result.type_mismatches.insert(
1039-
arg.into(),
1040-
TypeMismatch { expected: coercion_target, actual: ty.clone() },
1041-
);
1042-
}
1043-
}
969+
self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
1044970
}
1045971
}
1046972
}

crates/hir_ty/src/infer/unify.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,6 @@ impl<'a> InferenceTable<'a> {
302302
self.resolve_with_fallback(t, |_, _, d, _| d)
303303
}
304304

305-
/// This makes a copy of the given `t` where all unbound inference variables
306-
/// have been replaced by fresh ones. This is useful for 'speculatively'
307-
/// unifying the result with something, without affecting the original types.
308-
pub(crate) fn reinstantiate<T>(&mut self, t: T) -> T::Result
309-
where
310-
T: HasInterner<Interner = Interner> + Fold<Interner>,
311-
T::Result: HasInterner<Interner = Interner> + Fold<Interner, Result = T::Result>,
312-
{
313-
let canonicalized = self.canonicalize(t);
314-
self.var_unification_table.instantiate_canonical(&Interner, canonicalized.value)
315-
}
316-
317305
/// Unify two types and register new trait goals that arise from that.
318306
pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
319307
let result = if let Ok(r) = self.try_unify(ty1, ty2) {

crates/hir_ty/src/tests/coercion.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ fn test() {
390390
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
391391
//^^^^^^^^^ expected [usize], got [usize; 3]
392392
let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
393-
//^^^^^^^^^ expected [usize], got [usize; 3]
393+
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &Bar<[usize]>, got &Bar<[i32; 3]>
394394
}
395395
"#,
396396
);
@@ -522,7 +522,8 @@ fn main() {
522522

523523
#[test]
524524
fn coerce_unsize_expected_type_2() {
525-
check_no_mismatches(
525+
// FIXME: this is wrong, #9560
526+
check(
526527
r#"
527528
//- minicore: coerce_unsized
528529
struct InFile<T>;
@@ -539,48 +540,7 @@ fn test() {
539540
let x: InFile<()> = InFile;
540541
let n = &RecordField;
541542
takes_dyn(x.with_value(n));
542-
}
543-
"#,
544-
);
545-
}
546-
547-
#[test]
548-
fn coerce_unsize_expected_type_3() {
549-
check_no_mismatches(
550-
r#"
551-
//- minicore: coerce_unsized
552-
enum Option<T> { Some(T), None }
553-
struct RecordField;
554-
trait AstNode {}
555-
impl AstNode for RecordField {}
556-
557-
fn takes_dyn(it: Option<&dyn AstNode>) {}
558-
559-
fn test() {
560-
let x: InFile<()> = InFile;
561-
let n = &RecordField;
562-
takes_dyn(Option::Some(n));
563-
}
564-
"#,
565-
);
566-
}
567-
568-
#[test]
569-
fn coerce_unsize_expected_type_4() {
570-
check_no_mismatches(
571-
r#"
572-
//- minicore: coerce_unsized
573-
use core::{marker::Unsize, ops::CoerceUnsized};
574-
575-
struct B<T: ?Sized>(*const T);
576-
impl<T: ?Sized> B<T> {
577-
fn new(t: T) -> Self { B(&t) }
578-
}
579-
580-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
581-
582-
fn test() {
583-
let _: B<[isize]> = B::new({ [1, 2, 3] });
543+
// ^^^^^^^^^^^^^^^ expected InFile<&dyn AstNode>, got InFile<&RecordField>
584544
}
585545
"#,
586546
);

0 commit comments

Comments
 (0)