Fix inference of literals when the expectation is Castable

I followed the compiler: 5bce6d48ff/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs (L1560-L1579).
This commit is contained in:
Chayim Refael Friedman 2024-09-12 00:57:34 +03:00
parent c54a827f50
commit 5c42c08518
4 changed files with 51 additions and 9 deletions

View file

@ -895,21 +895,52 @@ impl InferenceContext<'_> {
TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(*int_ty)))
.intern(Interner)
}
None => self.table.new_integer_var(),
None => {
let expected_ty = expected.to_option(&mut self.table);
let opt_ty = match expected_ty.as_ref().map(|it| it.kind(Interner)) {
Some(TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))) => expected_ty,
Some(TyKind::Scalar(Scalar::Char)) => {
Some(TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(Interner))
}
Some(TyKind::Raw(..) | TyKind::FnDef(..) | TyKind::Function(..)) => {
Some(TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(Interner))
}
_ => None,
};
opt_ty.unwrap_or_else(|| self.table.new_integer_var())
}
},
Literal::Uint(_v, ty) => match ty {
Some(int_ty) => {
TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(*int_ty)))
.intern(Interner)
}
None => self.table.new_integer_var(),
None => {
let expected_ty = expected.to_option(&mut self.table);
let opt_ty = match expected_ty.as_ref().map(|it| it.kind(Interner)) {
Some(TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))) => expected_ty,
Some(TyKind::Scalar(Scalar::Char)) => {
Some(TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(Interner))
}
Some(TyKind::Raw(..) | TyKind::FnDef(..) | TyKind::Function(..)) => {
Some(TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(Interner))
}
_ => None,
};
opt_ty.unwrap_or_else(|| self.table.new_integer_var())
}
},
Literal::Float(_v, ty) => match ty {
Some(float_ty) => {
TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(*float_ty)))
.intern(Interner)
}
None => self.table.new_float_var(),
None => {
let opt_ty = expected.to_option(&mut self.table).filter(|ty| {
matches!(ty.kind(Interner), TyKind::Scalar(Scalar::Float(_)))
});
opt_ty.unwrap_or_else(|| self.table.new_float_var())
}
},
},
Expr::Underscore => {

View file

@ -49,7 +49,7 @@ fn let_stmt_coerce() {
//- minicore: coerce_unsized
fn test() {
let x: &[isize] = &[1];
// ^^^^ adjustments: Deref(None), Borrow(Ref('?3, Not)), Pointer(Unsize)
// ^^^^ adjustments: Deref(None), Borrow(Ref('?2, Not)), Pointer(Unsize)
let x: *const [isize] = &[1];
// ^^^^ adjustments: Deref(None), Borrow(RawPtr(Not)), Pointer(Unsize)
}
@ -148,7 +148,7 @@ fn foo<T>(x: &[T]) -> &[T] { x }
fn test(i: i32) {
let x = match i {
2 => foo(&[2]),
// ^^^^ adjustments: Deref(None), Borrow(Ref('?10, Not)), Pointer(Unsize)
// ^^^^ adjustments: Deref(None), Borrow(Ref('?8, Not)), Pointer(Unsize)
1 => &[1],
_ => &[3],
};

View file

@ -917,7 +917,7 @@ fn test(a: A<i32>) {
278..279 'A': extern "rust-call" A<i32>(*mut i32) -> A<i32>
278..292 'A(0 as *mut _)': A<i32>
278..307 'A(0 as...B(a)))': &'? i32
280..281 '0': i32
280..281 '0': usize
280..291 '0 as *mut _': *mut i32
297..306 '&&B(B(a))': &'? &'? B<B<A<i32>>>
298..306 '&B(B(a))': &'? B<B<A<i32>>>