diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 8bc3c50725d9..583d8a70d3be 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -1154,6 +1154,7 @@ impl<'a> InferenceContext<'a> { _ = self.infer_expr_coerce( self.body.body_expr, &Expectation::has_type(self.return_ty.clone()), + true, ) } } diff --git a/crates/hir-ty/src/infer/cast.rs b/crates/hir-ty/src/infer/cast.rs index caa3960a227c..2ceb9e05873e 100644 --- a/crates/hir-ty/src/infer/cast.rs +++ b/crates/hir-ty/src/infer/cast.rs @@ -127,7 +127,7 @@ impl CastCheck { return Ok(()); } - if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty) { + if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty, true) { apply_adjustments(self.source_expr, adj); set_coercion_cast(self.source_expr); return Ok(()); @@ -153,7 +153,7 @@ impl CastCheck { let sig = self.expr_ty.callable_sig(table.db).expect("FnDef had no sig"); let sig = table.normalize_associated_types_in(sig); let fn_ptr = TyKind::Function(sig.to_fn_ptr()).intern(Interner); - if let Ok((adj, _)) = table.coerce(&self.expr_ty, &fn_ptr) { + if let Ok((adj, _)) = table.coerce(&self.expr_ty, &fn_ptr, true) { apply_adjustments(self.source_expr, adj); } else { return Err(CastError::IllegalCast); @@ -240,7 +240,7 @@ impl CastCheck { if let TyKind::Array(ety, _) = t_expr.kind(Interner) { // Coerce to a raw pointer so that we generate RawPtr in MIR. let array_ptr_type = TyKind::Raw(m_expr, t_expr.clone()).intern(Interner); - if let Ok((adj, _)) = table.coerce(&self.expr_ty, &array_ptr_type) { + if let Ok((adj, _)) = table.coerce(&self.expr_ty, &array_ptr_type, true) { apply_adjustments(self.source_expr, adj); } else { never!( @@ -252,7 +252,7 @@ impl CastCheck { // This is a less strict condition than rustc's `demand_eqtype`, // but false negative is better than false positive - if table.coerce(ety, t_cast).is_ok() { + if table.coerce(ety, t_cast, true).is_ok() { return Ok(()); } } diff --git a/crates/hir-ty/src/infer/closure.rs b/crates/hir-ty/src/infer/closure.rs index 5cad08b93956..6ab0e5b44b7d 100644 --- a/crates/hir-ty/src/infer/closure.rs +++ b/crates/hir-ty/src/infer/closure.rs @@ -65,7 +65,7 @@ impl InferenceContext<'_> { } // Deduction from where-clauses in scope, as well as fn-pointer coercion are handled here. - let _ = self.coerce(Some(closure_expr), closure_ty, &expected_ty); + let _ = self.coerce(Some(closure_expr), closure_ty, &expected_ty, true); // Coroutines are not Fn* so return early. if matches!(closure_ty.kind(Interner), TyKind::Coroutine(..)) { diff --git a/crates/hir-ty/src/infer/coerce.rs b/crates/hir-ty/src/infer/coerce.rs index 7e758c0b5173..eba3128a6e59 100644 --- a/crates/hir-ty/src/infer/coerce.rs +++ b/crates/hir-ty/src/infer/coerce.rs @@ -139,8 +139,8 @@ impl CoerceMany { }; if let Some(sig) = sig { let target_ty = TyKind::Function(sig.to_fn_ptr()).intern(Interner); - let result1 = ctx.table.coerce_inner(self.merged_ty(), &target_ty); - let result2 = ctx.table.coerce_inner(expr_ty.clone(), &target_ty); + let result1 = ctx.table.coerce_inner(self.merged_ty(), &target_ty, true); + let result2 = ctx.table.coerce_inner(expr_ty.clone(), &target_ty, true); if let (Ok(result1), Ok(result2)) = (result1, result2) { ctx.table.register_infer_ok(InferOk { value: (), goals: result1.goals }); for &e in &self.expressions { @@ -159,9 +159,9 @@ impl CoerceMany { // type is a type variable and the new one is `!`, trying it the other // way around first would mean we make the type variable `!`, instead of // just marking it as possibly diverging. - if let Ok(res) = ctx.coerce(expr, &expr_ty, &self.merged_ty()) { + if let Ok(res) = ctx.coerce(expr, &expr_ty, &self.merged_ty(), true) { self.final_ty = Some(res); - } else if let Ok(res) = ctx.coerce(expr, &self.merged_ty(), &expr_ty) { + } else if let Ok(res) = ctx.coerce(expr, &self.merged_ty(), &expr_ty, true) { self.final_ty = Some(res); } else { match cause { @@ -197,7 +197,7 @@ pub(crate) fn coerce( let vars = table.fresh_subst(tys.binders.as_slice(Interner)); let ty1_with_vars = vars.apply(tys.value.0.clone(), Interner); let ty2_with_vars = vars.apply(tys.value.1.clone(), Interner); - let (adjustments, ty) = table.coerce(&ty1_with_vars, &ty2_with_vars)?; + let (adjustments, ty) = table.coerce(&ty1_with_vars, &ty2_with_vars, true)?; // default any type vars that weren't unified back to their original bound vars // (kind of hacky) let find_var = |iv| { @@ -227,10 +227,16 @@ impl InferenceContext<'_> { expr: Option, from_ty: &Ty, to_ty: &Ty, + // [Comment from rustc](https://github.com/rust-lang/rust/blob/4cc494bbfe9911d24f3ee521f98d5c6bb7e3ffe8/compiler/rustc_hir_typeck/src/coercion.rs#L85-L89) + // Whether we allow `NeverToAny` coercions. This is unsound if we're + // coercing a place expression without it counting as a read in the MIR. + // This is a side-effect of HIR not really having a great distinction + // between places and values. + coerce_never: bool, ) -> Result { let from_ty = self.resolve_ty_shallow(from_ty); let to_ty = self.resolve_ty_shallow(to_ty); - let (adjustments, ty) = self.table.coerce(&from_ty, &to_ty)?; + let (adjustments, ty) = self.table.coerce(&from_ty, &to_ty, coerce_never)?; if let Some(expr) = expr { self.write_expr_adj(expr, adjustments); } @@ -245,10 +251,11 @@ impl InferenceTable<'_> { &mut self, from_ty: &Ty, to_ty: &Ty, + coerce_never: bool, ) -> Result<(Vec, Ty), TypeError> { let from_ty = self.resolve_ty_shallow(from_ty); let to_ty = self.resolve_ty_shallow(to_ty); - match self.coerce_inner(from_ty, &to_ty) { + match self.coerce_inner(from_ty, &to_ty, coerce_never) { Ok(InferOk { value: (adjustments, ty), goals }) => { self.register_infer_ok(InferOk { value: (), goals }); Ok((adjustments, ty)) @@ -260,19 +267,23 @@ impl InferenceTable<'_> { } } - fn coerce_inner(&mut self, from_ty: Ty, to_ty: &Ty) -> CoerceResult { + fn coerce_inner(&mut self, from_ty: Ty, to_ty: &Ty, coerce_never: bool) -> CoerceResult { if from_ty.is_never() { - // Subtle: If we are coercing from `!` to `?T`, where `?T` is an unbound - // type variable, we want `?T` to fallback to `!` if not - // otherwise constrained. An example where this arises: - // - // let _: Option = Some({ return; }); - // - // here, we would coerce from `!` to `?T`. if let TyKind::InferenceVar(tv, TyVariableKind::General) = to_ty.kind(Interner) { self.set_diverging(*tv, true); } - return success(simple(Adjust::NeverToAny)(to_ty.clone()), to_ty.clone(), vec![]); + if coerce_never { + // Subtle: If we are coercing from `!` to `?T`, where `?T` is an unbound + // type variable, we want `?T` to fallback to `!` if not + // otherwise constrained. An example where this arises: + // + // let _: Option = Some({ return; }); + // + // here, we would coerce from `!` to `?T`. + return success(simple(Adjust::NeverToAny)(to_ty.clone()), to_ty.clone(), vec![]); + } else { + return self.unify_and(&from_ty, to_ty, identity); + } } // If we are coercing into a TAIT, coerce into its proxy inference var, instead. diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index a04e7b17ae6e..2f6eb543328d 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -10,10 +10,11 @@ use either::Either; use hir_def::{ hir::{ ArithOp, Array, AsmOperand, AsmOptions, BinaryOp, ClosureKind, Expr, ExprId, LabelId, - Literal, Statement, UnaryOp, + Literal, Pat, PatId, Statement, UnaryOp, }, lang_item::{LangItem, LangItemTarget}, path::{GenericArg, GenericArgs, Path}, + resolver::ValueNs, BlockId, FieldId, GenericDefId, GenericParamId, ItemContainerId, Lookup, TupleFieldId, TupleId, }; use hir_expand::name::Name; @@ -53,8 +54,13 @@ use super::{ }; impl InferenceContext<'_> { - pub(crate) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { - let ty = self.infer_expr_inner(tgt_expr, expected); + pub(crate) fn infer_expr( + &mut self, + tgt_expr: ExprId, + expected: &Expectation, + is_read: bool, + ) -> Ty { + let ty = self.infer_expr_inner(tgt_expr, expected, is_read); if let Some(expected_ty) = expected.only_has_type(&mut self.table) { let could_unify = self.unify(&ty, &expected_ty); if !could_unify { @@ -67,16 +73,22 @@ impl InferenceContext<'_> { ty } - pub(crate) fn infer_expr_no_expect(&mut self, tgt_expr: ExprId) -> Ty { - self.infer_expr_inner(tgt_expr, &Expectation::None) + pub(crate) fn infer_expr_no_expect(&mut self, tgt_expr: ExprId, is_read: bool) -> Ty { + self.infer_expr_inner(tgt_expr, &Expectation::None, is_read) } /// Infer type of expression with possibly implicit coerce to the expected type. /// Return the type after possible coercion. - pub(super) fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty { - let ty = self.infer_expr_inner(expr, expected); + pub(super) fn infer_expr_coerce( + &mut self, + expr: ExprId, + expected: &Expectation, + is_read: bool, + ) -> Ty { + let ty = self.infer_expr_inner(expr, expected, is_read); if let Some(target) = expected.only_has_type(&mut self.table) { - match self.coerce(Some(expr), &ty, &target) { + let coerce_never = self.expr_guaranteed_to_constitute_read_for_never(expr, is_read); + match self.coerce(Some(expr), &ty, &target, coerce_never) { Ok(res) => res, Err(_) => { self.result.type_mismatches.insert( @@ -91,8 +103,130 @@ impl InferenceContext<'_> { } } - fn infer_expr_coerce_never(&mut self, expr: ExprId, expected: &Expectation) -> Ty { - let ty = self.infer_expr_inner(expr, expected); + /// Whether this expression constitutes a read of value of the type that + /// it evaluates to. + /// + /// This is used to determine if we should consider the block to diverge + /// if the expression evaluates to `!`, and if we should insert a `NeverToAny` + /// coercion for values of type `!`. + /// + /// This function generally returns `false` if the expression is a place + /// expression and the *parent* expression is the scrutinee of a match or + /// the pointee of an `&` addr-of expression, since both of those parent + /// expressions take a *place* and not a value. + pub(super) fn expr_guaranteed_to_constitute_read_for_never( + &mut self, + expr: ExprId, + is_read: bool, + ) -> bool { + // We only care about place exprs. Anything else returns an immediate + // which would constitute a read. We don't care about distinguishing + // "syntactic" place exprs since if the base of a field projection is + // not a place then it would've been UB to read from it anyways since + // that constitutes a read. + if !self.is_syntatic_place_expr(expr) { + return true; + } + + // rustc queries parent hir node of `expr` here and determine whether + // the current `expr` is read of value per its parent. + // But since we don't have hir node, we cannot follow such "bottom-up" + // method. + // So, we pass down such readness from the parent expression through the + // recursive `infer_expr*` calls in a "top-down" manner. + is_read + } + + /// Whether this pattern constitutes a read of value of the scrutinee that + /// it is matching against. This is used to determine whether we should + /// perform `NeverToAny` coercions. + fn pat_guaranteed_to_constitute_read_for_never(&self, pat: PatId) -> bool { + match &self.body[pat] { + // Does not constitute a read. + Pat::Wild => false, + + // This is unnecessarily restrictive when the pattern that doesn't + // constitute a read is unreachable. + // + // For example `match *never_ptr { value => {}, _ => {} }` or + // `match *never_ptr { _ if false => {}, value => {} }`. + // + // It is however fine to be restrictive here; only returning `true` + // can lead to unsoundness. + Pat::Or(subpats) => { + subpats.iter().all(|pat| self.pat_guaranteed_to_constitute_read_for_never(*pat)) + } + + // All of these constitute a read, or match on something that isn't `!`, + // which would require a `NeverToAny` coercion. + Pat::Bind { .. } + | Pat::TupleStruct { .. } + | Pat::Path(_) + | Pat::Tuple { .. } + | Pat::Box { .. } + | Pat::Ref { .. } + | Pat::Lit(_) + | Pat::Range { .. } + | Pat::Slice { .. } + | Pat::ConstBlock(_) + | Pat::Record { .. } + | Pat::Missing => true, + } + } + + fn is_syntatic_place_expr(&self, expr: ExprId) -> bool { + match &self.body[expr] { + // Lang item paths cannot currently be local variables or statics. + Expr::Path(Path::LangItem(_, _)) => false, + Expr::Path(Path::Normal { type_anchor: Some(_), .. }) => false, + Expr::Path(path) => self + .resolver + .resolve_path_in_value_ns_fully(self.db.upcast(), path) + .map_or(true, |res| matches!(res, ValueNs::LocalBinding(_) | ValueNs::StaticId(_))), + Expr::Underscore => true, + Expr::UnaryOp { op: UnaryOp::Deref, .. } => true, + Expr::Field { .. } | Expr::Index { .. } => true, + Expr::Call { .. } + | Expr::MethodCall { .. } + | Expr::Tuple { .. } + | Expr::If { .. } + | Expr::Match { .. } + | Expr::Closure { .. } + | Expr::Block { .. } + | Expr::Array(..) + | Expr::Break { .. } + | Expr::Continue { .. } + | Expr::Return { .. } + | Expr::Become { .. } + | Expr::Let { .. } + | Expr::Loop { .. } + | Expr::InlineAsm(..) + | Expr::OffsetOf(..) + | Expr::Literal(..) + | Expr::Const(..) + | Expr::UnaryOp { .. } + | Expr::BinaryOp { .. } + | Expr::Yield { .. } + | Expr::Cast { .. } + | Expr::Async { .. } + | Expr::Unsafe { .. } + | Expr::Await { .. } + | Expr::Ref { .. } + | Expr::Range { .. } + | Expr::Box { .. } + | Expr::RecordLit { .. } + | Expr::Yeet { .. } + | Expr::Missing => false, + } + } + + fn infer_expr_coerce_never( + &mut self, + expr: ExprId, + expected: &Expectation, + is_read: bool, + ) -> Ty { + let ty = self.infer_expr_inner(expr, expected, is_read); // While we don't allow *arbitrary* coercions here, we *do* allow // coercions from `!` to `expected`. if ty.is_never() { @@ -105,7 +239,7 @@ impl InferenceContext<'_> { } if let Some(target) = expected.only_has_type(&mut self.table) { - self.coerce(Some(expr), &ty, &target) + self.coerce(Some(expr), &ty, &target, true) .expect("never-to-any coercion should always succeed") } else { ty @@ -124,7 +258,7 @@ impl InferenceContext<'_> { } } - fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { + fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation, is_read: bool) -> Ty { self.db.unwind_if_cancelled(); let ty = match &self.body[tgt_expr] { @@ -134,17 +268,18 @@ impl InferenceContext<'_> { self.infer_expr_coerce_never( condition, &Expectation::HasType(self.result.standard_types.bool_.clone()), + true, ); let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); - let then_ty = self.infer_expr_inner(then_branch, expected); + let then_ty = self.infer_expr_inner(then_branch, expected, true); let then_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); let mut coerce = CoerceMany::new(expected.coercion_target_type(&mut self.table)); coerce.coerce(self, Some(then_branch), &then_ty, CoercionCause::Expr(then_branch)); match else_branch { Some(else_branch) => { - let else_ty = self.infer_expr_inner(else_branch, expected); + let else_ty = self.infer_expr_inner(else_branch, expected, true); let else_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); coerce.coerce( self, @@ -163,7 +298,8 @@ impl InferenceContext<'_> { coerce.complete(self) } &Expr::Let { pat, expr } => { - let input_ty = self.infer_expr(expr, &Expectation::none()); + let child_is_read = self.pat_guaranteed_to_constitute_read_for_never(pat); + let input_ty = self.infer_expr(expr, &Expectation::none(), child_is_read); self.infer_top_pat(pat, &input_ty); self.result.standard_types.bool_.clone() } @@ -176,7 +312,7 @@ impl InferenceContext<'_> { Expr::Const(id) => { self.with_breakable_ctx(BreakableKind::Border, None, None, |this| { let loc = this.db.lookup_intern_anonymous_const(*id); - this.infer_expr(loc.root, expected) + this.infer_expr(loc.root, expected, true) }) .1 } @@ -189,7 +325,7 @@ impl InferenceContext<'_> { let ty = self.table.new_type_var(); let (breaks, ()) = self.with_breakable_ctx(BreakableKind::Loop, Some(ty), label, |this| { - this.infer_expr(body, &Expectation::HasType(TyBuilder::unit())); + this.infer_expr(body, &Expectation::HasType(TyBuilder::unit()), true); }); match breaks { @@ -312,7 +448,7 @@ impl InferenceContext<'_> { ty } Expr::Call { callee, args, .. } => { - let callee_ty = self.infer_expr(*callee, &Expectation::none()); + let callee_ty = self.infer_expr(*callee, &Expectation::none(), true); let mut derefs = Autoderef::new(&mut self.table, callee_ty.clone(), false); let (res, derefed_callee) = loop { let Some((callee_deref_ty, _)) = derefs.next() else { @@ -393,7 +529,10 @@ impl InferenceContext<'_> { expected, ), Expr::Match { expr, arms } => { - let input_ty = self.infer_expr(*expr, &Expectation::none()); + let scrutinee_is_read = arms + .iter() + .all(|arm| self.pat_guaranteed_to_constitute_read_for_never(arm.pat)); + let input_ty = self.infer_expr(*expr, &Expectation::none(), scrutinee_is_read); if arms.is_empty() { self.diverges = Diverges::Always; @@ -423,11 +562,12 @@ impl InferenceContext<'_> { self.infer_expr_coerce_never( guard_expr, &Expectation::HasType(self.result.standard_types.bool_.clone()), + true, ); } self.diverges = Diverges::Maybe; - let arm_ty = self.infer_expr_inner(arm.expr, &expected); + let arm_ty = self.infer_expr_inner(arm.expr, &expected, true); all_arms_diverge &= self.diverges; coerce.coerce(self, Some(arm.expr), &arm_ty, CoercionCause::Expr(arm.expr)); } @@ -480,7 +620,7 @@ impl InferenceContext<'_> { }, None => self.err_ty(), }; - self.infer_expr_inner(expr, &Expectation::HasType(opt_coerce_to)) + self.infer_expr_inner(expr, &Expectation::HasType(opt_coerce_to), true) } else { TyBuilder::unit() }; @@ -517,10 +657,10 @@ impl InferenceContext<'_> { Expr::Yield { expr } => { if let Some((resume_ty, yield_ty)) = self.resume_yield_tys.clone() { if let Some(expr) = expr { - self.infer_expr_coerce(*expr, &Expectation::has_type(yield_ty)); + self.infer_expr_coerce(*expr, &Expectation::has_type(yield_ty), true); } else { let unit = self.result.standard_types.unit.clone(); - let _ = self.coerce(Some(tgt_expr), &unit, &yield_ty); + let _ = self.coerce(Some(tgt_expr), &unit, &yield_ty, true); } resume_ty } else { @@ -530,7 +670,7 @@ impl InferenceContext<'_> { } Expr::Yeet { expr } => { if let &Some(expr) = expr { - self.infer_expr_no_expect(expr); + self.infer_expr_no_expect(expr, true); } self.result.standard_types.never.clone() } @@ -589,28 +729,33 @@ impl InferenceContext<'_> { // Field type might have some unknown types // FIXME: we may want to emit a single type variable for all instance of type fields? let field_ty = self.insert_type_vars(field_ty); - self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); + self.infer_expr_coerce( + field.expr, + &Expectation::has_type(field_ty), + true, + ); } } None => { for field in fields.iter() { - self.infer_expr_coerce(field.expr, &Expectation::None); + // Field projections don't constitute reads. + self.infer_expr_coerce(field.expr, &Expectation::None, false); } } } if let Some(expr) = spread { - self.infer_expr(*expr, &Expectation::has_type(ty.clone())); + self.infer_expr(*expr, &Expectation::has_type(ty.clone()), true); } ty } Expr::Field { expr, name } => self.infer_field_access(tgt_expr, *expr, name, expected), Expr::Await { expr } => { - let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); + let inner_ty = self.infer_expr_inner(*expr, &Expectation::none(), true); self.resolve_associated_type(inner_ty, self.resolve_future_future_output()) } Expr::Cast { expr, type_ref } => { let cast_ty = self.make_ty(type_ref); - let expr_ty = self.infer_expr(*expr, &Expectation::Castable(cast_ty.clone())); + let expr_ty = self.infer_expr(*expr, &Expectation::Castable(cast_ty.clone()), true); self.deferred_cast_checks.push(CastCheck::new( tgt_expr, *expr, @@ -638,7 +783,7 @@ impl InferenceContext<'_> { } else { Expectation::none() }; - let inner_ty = self.infer_expr_inner(*expr, &expectation); + let inner_ty = self.infer_expr_inner(*expr, &expectation, true); match rawness { Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), Rawness::Ref => { @@ -650,7 +795,7 @@ impl InferenceContext<'_> { } &Expr::Box { expr } => self.infer_expr_box(expr, expected), Expr::UnaryOp { expr, op } => { - let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); + let inner_ty = self.infer_expr_inner(*expr, &Expectation::none(), true); let inner_ty = self.resolve_ty_shallow(&inner_ty); // FIXME: Note down method resolution her match op { @@ -720,19 +865,20 @@ impl InferenceContext<'_> { // cannot happen in destructuring assignments because of how // they are desugared. if is_ordinary { - let lhs_ty = self.infer_expr(lhs, &Expectation::none()); - self.infer_expr_coerce(*rhs, &Expectation::has_type(lhs_ty)); + // LHS of assignment doesn't constitute reads. + let lhs_ty = self.infer_expr(lhs, &Expectation::none(), false); + self.infer_expr_coerce(*rhs, &Expectation::has_type(lhs_ty), false); } else { - let rhs_ty = self.infer_expr(*rhs, &Expectation::none()); + let rhs_ty = self.infer_expr(*rhs, &Expectation::none(), true); self.infer_assignee_expr(lhs, &rhs_ty); } self.result.standard_types.unit.clone() } Some(BinaryOp::LogicOp(_)) => { let bool_ty = self.result.standard_types.bool_.clone(); - self.infer_expr_coerce(*lhs, &Expectation::HasType(bool_ty.clone())); + self.infer_expr_coerce(*lhs, &Expectation::HasType(bool_ty.clone()), true); let lhs_diverges = self.diverges; - self.infer_expr_coerce(*rhs, &Expectation::HasType(bool_ty.clone())); + self.infer_expr_coerce(*rhs, &Expectation::HasType(bool_ty.clone()), true); // Depending on the LHS' value, the RHS can never execute. self.diverges = lhs_diverges; bool_ty @@ -741,11 +887,11 @@ impl InferenceContext<'_> { _ => self.err_ty(), }, Expr::Range { lhs, rhs, range_type } => { - let lhs_ty = lhs.map(|e| self.infer_expr_inner(e, &Expectation::none())); + let lhs_ty = lhs.map(|e| self.infer_expr_inner(e, &Expectation::none(), true)); let rhs_expect = lhs_ty .as_ref() .map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone())); - let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); + let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect, true)); match (range_type, lhs_ty, rhs_ty) { (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { Some(adt) => TyBuilder::adt(self.db, adt).build(), @@ -779,8 +925,8 @@ impl InferenceContext<'_> { } } Expr::Index { base, index, is_assignee_expr } => { - let base_ty = self.infer_expr_inner(*base, &Expectation::none()); - let index_ty = self.infer_expr(*index, &Expectation::none()); + let base_ty = self.infer_expr_inner(*base, &Expectation::none(), true); + let index_ty = self.infer_expr(*index, &Expectation::none(), true); if let Some(index_trait) = self.resolve_lang_trait(LangItem::Index) { let canonicalized = self.canonicalize(base_ty.clone()); @@ -851,7 +997,7 @@ impl InferenceContext<'_> { }; for (expr, ty) in exprs.iter().zip(tys.iter_mut()) { - *ty = self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); + *ty = self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()), true); } TyKind::Tuple(tys.len(), Substitution::from_iter(Interner, tys)).intern(Interner) @@ -958,7 +1104,7 @@ impl InferenceContext<'_> { Expr::OffsetOf(_) => TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(Interner), Expr::InlineAsm(asm) => { let mut check_expr_asm_operand = |expr, is_input: bool| { - let ty = self.infer_expr_no_expect(expr); + let ty = self.infer_expr_no_expect(expr, true); // If this is an input value, we require its type to be fully resolved // at this point. This allows us to provide helpful coercions which help @@ -975,11 +1121,11 @@ impl InferenceContext<'_> { CallableSig::from_def(self.db, *def, parameters).to_fn_ptr(), ) .intern(Interner); - _ = self.coerce(Some(expr), &ty, &fnptr_ty); + _ = self.coerce(Some(expr), &ty, &fnptr_ty, true); } TyKind::Ref(mutbl, _, base_ty) => { let ptr_ty = TyKind::Raw(*mutbl, base_ty.clone()).intern(Interner); - _ = self.coerce(Some(expr), &ty, &ptr_ty); + _ = self.coerce(Some(expr), &ty, &ptr_ty, true); } _ => {} } @@ -1016,7 +1162,9 @@ impl InferenceContext<'_> { // use a new type variable if we got unknown here let ty = self.insert_type_vars_shallow(ty); self.write_expr_ty(tgt_expr, ty.clone()); - if self.resolve_ty_shallow(&ty).is_never() { + if self.resolve_ty_shallow(&ty).is_never() + && self.expr_guaranteed_to_constitute_read_for_never(tgt_expr, is_read) + { // Any expression that produces a value of type `!` must have diverged self.diverges = Diverges::Always; } @@ -1041,7 +1189,7 @@ impl InferenceContext<'_> { let (_, inner_ty) = self.with_breakable_ctx(BreakableKind::Border, None, None, |this| { let ty = this.infer_block(tgt_expr, *id, statements, *tail, None, expected); if let Some(target) = expected.only_has_type(&mut this.table) { - match this.coerce(Some(tgt_expr), &ty, &target) { + match this.coerce(Some(tgt_expr), &ty, &target, true) { Ok(res) => res, Err(_) => { this.result.type_mismatches.insert( @@ -1153,7 +1301,7 @@ impl InferenceContext<'_> { Array::ElementList { elements, .. } => { let mut coerce = CoerceMany::new(elem_ty); for &expr in elements.iter() { - let cur_elem_ty = self.infer_expr_inner(expr, &expected); + let cur_elem_ty = self.infer_expr_inner(expr, &expected, true); coerce.coerce(self, Some(expr), &cur_elem_ty, CoercionCause::Expr(expr)); } ( @@ -1162,13 +1310,13 @@ impl InferenceContext<'_> { ) } &Array::Repeat { initializer, repeat } => { - self.infer_expr_coerce(initializer, &Expectation::has_type(elem_ty.clone())); + self.infer_expr_coerce(initializer, &Expectation::has_type(elem_ty.clone()), true); let usize = TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(Interner); match self.body[repeat] { Expr::Underscore => { self.write_expr_ty(repeat, usize); } - _ => _ = self.infer_expr(repeat, &Expectation::HasType(usize)), + _ => _ = self.infer_expr(repeat, &Expectation::HasType(usize), true), } ( @@ -1193,7 +1341,7 @@ impl InferenceContext<'_> { .as_mut() .expect("infer_return called outside function body") .expected_ty(); - let return_expr_ty = self.infer_expr_inner(expr, &Expectation::HasType(ret_ty)); + let return_expr_ty = self.infer_expr_inner(expr, &Expectation::HasType(ret_ty), true); let mut coerce_many = self.return_coercion.take().unwrap(); coerce_many.coerce(self, Some(expr), &return_expr_ty, CoercionCause::Expr(expr)); self.return_coercion = Some(coerce_many); @@ -1213,7 +1361,7 @@ impl InferenceContext<'_> { None => { // FIXME: diagnose return outside of function if let Some(expr) = expr { - self.infer_expr_no_expect(expr); + self.infer_expr_no_expect(expr, true); } } } @@ -1226,7 +1374,7 @@ impl InferenceContext<'_> { let ret_ty = return_coercion.expected_ty(); let call_expr_ty = - self.infer_expr_inner(expr, &Expectation::HasType(ret_ty.clone())); + self.infer_expr_inner(expr, &Expectation::HasType(ret_ty.clone()), true); // NB: this should *not* coerce. // tail calls don't support any coercions except lifetimes ones (like `&'static u8 -> &'a u8`). @@ -1234,7 +1382,7 @@ impl InferenceContext<'_> { } None => { // FIXME: diagnose `become` outside of functions - self.infer_expr_no_expect(expr); + self.infer_expr_no_expect(expr, true); } } @@ -1255,7 +1403,7 @@ impl InferenceContext<'_> { }) .unwrap_or_else(Expectation::none); - let inner_ty = self.infer_expr_inner(inner_expr, &inner_exp); + let inner_ty = self.infer_expr_inner(inner_expr, &inner_exp, true); TyBuilder::adt(self.db, box_id) .push(inner_ty) .fill_with_defaults(self.db, || self.table.new_type_var()) @@ -1333,12 +1481,13 @@ impl InferenceContext<'_> { Expr::Underscore => rhs_ty.clone(), _ => { // `lhs` is a place expression, a unit struct, or an enum variant. - let lhs_ty = self.infer_expr_inner(lhs, &Expectation::none()); + // LHS of assignment doesn't constitute reads. + let lhs_ty = self.infer_expr_inner(lhs, &Expectation::none(), false); // This is the only branch where this function may coerce any type. // We are returning early to avoid the unifiability check below. let lhs_ty = self.insert_type_vars_shallow(lhs_ty); - let ty = match self.coerce(None, &rhs_ty, &lhs_ty) { + let ty = match self.coerce(None, &rhs_ty, &lhs_ty, true) { Ok(ty) => ty, Err(_) => { self.result.type_mismatches.insert( @@ -1373,7 +1522,8 @@ impl InferenceContext<'_> { tgt_expr: ExprId, ) -> Ty { let lhs_expectation = Expectation::none(); - let lhs_ty = self.infer_expr(lhs, &lhs_expectation); + let lhs_ty = + self.infer_expr(lhs, &lhs_expectation, matches!(op, BinaryOp::Assignment { .. })); let rhs_ty = self.table.new_type_var(); let trait_func = lang_items_for_bin_op(op).and_then(|(name, lang_item)| { @@ -1396,7 +1546,7 @@ impl InferenceContext<'_> { self.err_ty() }; - self.infer_expr_coerce(rhs, &Expectation::has_type(rhs_ty)); + self.infer_expr_coerce(rhs, &Expectation::has_type(rhs_ty), true); return ret_ty; } @@ -1415,7 +1565,7 @@ impl InferenceContext<'_> { let method_ty = self.db.value_ty(func.into()).unwrap().substitute(Interner, &subst); self.register_obligations_for_call(&method_ty); - self.infer_expr_coerce(rhs, &Expectation::has_type(rhs_ty.clone())); + self.infer_expr_coerce(rhs, &Expectation::has_type(rhs_ty.clone()), true); let ret_ty = match method_ty.callable_sig(self.db) { Some(sig) => { @@ -1487,12 +1637,21 @@ impl InferenceContext<'_> { .unwrap_or_else(|| this.table.new_type_var()); let ty = if let Some(expr) = initializer { + // If we have a subpattern that performs a read, we want to consider this + // to diverge for compatibility to support something like `let x: () = *never_ptr;`. + let target_is_read = + this.pat_guaranteed_to_constitute_read_for_never(*pat); let ty = if contains_explicit_ref_binding(this.body, *pat) { - this.infer_expr(*expr, &Expectation::has_type(decl_ty.clone())) + this.infer_expr( + *expr, + &Expectation::has_type(decl_ty.clone()), + target_is_read, + ) } else { this.infer_expr_coerce( *expr, &Expectation::has_type(decl_ty.clone()), + target_is_read, ) }; if type_ref.is_some() { @@ -1512,17 +1671,19 @@ impl InferenceContext<'_> { this.infer_expr_coerce( *expr, &Expectation::HasType(this.result.standard_types.never.clone()), + true, ); this.diverges = previous_diverges; } } &Statement::Expr { expr, has_semi } => { if has_semi { - this.infer_expr(expr, &Expectation::none()); + this.infer_expr(expr, &Expectation::none(), true); } else { this.infer_expr_coerce( expr, &Expectation::HasType(this.result.standard_types.unit.clone()), + true, ); } } @@ -1532,7 +1693,7 @@ impl InferenceContext<'_> { // FIXME: This should make use of the breakable CoerceMany if let Some(expr) = tail { - this.infer_expr_coerce(expr, expected) + this.infer_expr_coerce(expr, expected, true) } else { // Citing rustc: if there is no explicit tail expression, // that is typically equivalent to a tail expression @@ -1545,8 +1706,15 @@ impl InferenceContext<'_> { // we don't even make an attempt at coercion this.table.new_maybe_never_var() } else if let Some(t) = expected.only_has_type(&mut this.table) { + let coerce_never = + this.expr_guaranteed_to_constitute_read_for_never(expr, true); if this - .coerce(Some(expr), &this.result.standard_types.unit.clone(), &t) + .coerce( + Some(expr), + &this.result.standard_types.unit.clone(), + &t, + coerce_never, + ) .is_err() { this.result.type_mismatches.insert( @@ -1658,7 +1826,8 @@ impl InferenceContext<'_> { name: &Name, expected: &Expectation, ) -> Ty { - let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none()); + // Field projections don't constitute reads. + let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none(), false); if name.is_missing() { // Bail out early, don't even try to look up field. Also, we don't issue an unresolved @@ -1730,7 +1899,7 @@ impl InferenceContext<'_> { generic_args: Option<&GenericArgs>, expected: &Expectation, ) -> Ty { - let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none()); + let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none(), true); let canonicalized_receiver = self.canonicalize(receiver_ty.clone()); let resolved = method_resolution::lookup_method( @@ -1917,7 +2086,7 @@ impl InferenceContext<'_> { let expected_ty = self.normalize_associated_types_in(expected_ty); let expected = Expectation::rvalue_hint(self, expected_ty); // infer with the expected type we have... - let ty = self.infer_expr_inner(arg, &expected); + let ty = self.infer_expr_inner(arg, &expected, true); // then coerce to either the expected type or just the formal parameter type let coercion_target = if let Some(ty) = expected.only_has_type(&mut self.table) { @@ -1931,7 +2100,15 @@ impl InferenceContext<'_> { // The function signature may contain some unknown types, so we need to insert // type vars here to avoid type mismatch false positive. let coercion_target = self.insert_type_vars(coercion_target); - if self.coerce(Some(arg), &ty, &coercion_target).is_err() && !arg_count_mismatch { + + // Any expression that produces a value of type `!` must have diverged, + // unless it's a place expression that isn't being read from, in which case + // diverging would be unsound since we may never actually read the `!`. + // e.g. `let _ = *never_ptr;` with `never_ptr: *const !`. + let coerce_never = self.expr_guaranteed_to_constitute_read_for_never(arg, true); + if self.coerce(Some(arg), &ty, &coercion_target, coerce_never).is_err() + && !arg_count_mismatch + { self.result.type_mismatches.insert( arg.into(), TypeMismatch { expected: coercion_target, actual: ty.clone() }, @@ -2106,7 +2283,7 @@ impl InferenceContext<'_> { } let _ty = arg.data(Interner).ty.clone(); let expected = Expectation::none(); // FIXME use actual const ty, when that is lowered correctly - self.infer_expr(args[arg_idx as usize], &expected); + self.infer_expr(args[arg_idx as usize], &expected, true); // FIXME: evaluate and unify with the const } let mut indices = legacy_const_generics_indices.as_ref().clone(); diff --git a/crates/hir-ty/src/infer/mutability.rs b/crates/hir-ty/src/infer/mutability.rs index 8e52725e5360..b6ba195a6fa8 100644 --- a/crates/hir-ty/src/infer/mutability.rs +++ b/crates/hir-ty/src/infer/mutability.rs @@ -164,7 +164,7 @@ impl InferenceContext<'_> { if let Some(ty) = self.result.type_of_expr.get(index) { ty.clone() } else { - self.infer_expr(index, &Expectation::none()) + self.infer_expr(index, &Expectation::none(), true) }; let trait_ref = TyBuilder::trait_ref(self.db, index_trait) .push(base_ty) diff --git a/crates/hir-ty/src/infer/pat.rs b/crates/hir-ty/src/infer/pat.rs index 50542b2acd45..ca238c664597 100644 --- a/crates/hir-ty/src/infer/pat.rs +++ b/crates/hir-ty/src/infer/pat.rs @@ -361,7 +361,7 @@ impl InferenceContext<'_> { None => self.err_ty(), }, Pat::ConstBlock(expr) => { - self.infer_expr(*expr, &Expectation::has_type(expected.clone())) + self.infer_expr(*expr, &Expectation::has_type(expected.clone()), true) } Pat::Missing => self.err_ty(), }; @@ -497,7 +497,7 @@ impl InferenceContext<'_> { } } - self.infer_expr(expr, &Expectation::has_type(expected.clone())) + self.infer_expr(expr, &Expectation::has_type(expected.clone()), true) } fn is_non_ref_pat(&mut self, body: &hir_def::body::Body, pat: PatId) -> bool { diff --git a/crates/hir-ty/src/tests/never_type.rs b/crates/hir-ty/src/tests/never_type.rs index 5c63cd00f97d..1ca4c9b2ad5a 100644 --- a/crates/hir-ty/src/tests/never_type.rs +++ b/crates/hir-ty/src/tests/never_type.rs @@ -539,3 +539,249 @@ fn test() { "#, ); } + +#[test] +fn diverging_place_match1() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn not_a_read() -> ! { + unsafe { + let x: *const ! = 0 as _; + let _: ! = *x; + } +} +"#, + expect![[r#" + 21..100 '{ ... } }': ! + 27..98 'unsafe... }': ! + 48..49 'x': *const ! + 62..63 '0': i32 + 62..68 '0 as _': *const ! + 82..83 '_': ! + 89..91 '*x': ! + 90..91 'x': *const ! + 27..98: expected !, got () + "#]], + ) +} + +#[test] +fn diverging_place_match2() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn not_a_read_implicit() -> ! { + unsafe { + let x: *const ! = 0 as _; + let _ = *x; + } +} +"#, + expect![[r#" + 30..106 '{ ... } }': ! + 36..104 'unsafe... }': ! + 57..58 'x': *const ! + 71..72 '0': i32 + 71..77 '0 as _': *const ! + 91..92 '_': ! + 95..97 '*x': ! + 96..97 'x': *const ! + 36..104: expected !, got () + "#]], + ) +} + +#[test] +fn diverging_place_match3() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn not_a_read_guide_coercion() -> ! { + unsafe { + let x: *const ! = 0 as _; + let _: () = *x; + } +} +"#, + expect![[r#" + 36..116 '{ ... } }': ! + 42..114 'unsafe... }': ! + 63..64 'x': *const ! + 77..78 '0': i32 + 77..83 '0 as _': *const ! + 97..98 '_': () + 105..107 '*x': ! + 106..107 'x': *const ! + 42..114: expected !, got () + 105..107: expected (), got ! + "#]], + ) +} + +#[test] +fn diverging_place_match4() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn empty_match() -> ! { + unsafe { + let x: *const ! = 0 as _; + match *x { _ => {} }; + } +} +"#, + expect![[r#" + 22..108 '{ ... } }': ! + 28..106 'unsafe... }': ! + 49..50 'x': *const ! + 63..64 '0': i32 + 63..69 '0 as _': *const ! + 79..99 'match ...> {} }': () + 85..87 '*x': ! + 86..87 'x': *const ! + 90..91 '_': ! + 95..97 '{}': () + 28..106: expected !, got () + "#]], + ) +} + +#[test] +fn diverging_place_match5() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn field_projection() -> ! { + unsafe { + let x: *const (!, ()) = 0 as _; + let _ = (*x).0; + } +} +"#, + expect![[r#" + 27..113 '{ ... } }': ! + 33..111 'unsafe... }': ! + 54..55 'x': *const (!, ()) + 74..75 '0': i32 + 74..80 '0 as _': *const (!, ()) + 94..95 '_': ! + 98..104 '(*x).0': ! + 99..101 '*x': (!, ()) + 100..101 'x': *const (!, ()) + 33..111: expected !, got () + "#]], + ) +} + +#[test] +fn diverging_place_match6() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn covered_arm() -> ! { + unsafe { + let x: *const ! = 0 as _; + let (_ | 1i32) = *x; + } +} +"#, + expect![[r#" + 22..107 '{ ... } }': ! + 28..105 'unsafe... }': ! + 49..50 'x': *const ! + 63..64 '0': i32 + 63..69 '0 as _': *const ! + 84..85 '_': ! + 84..92 '_ | 1i32': ! + 88..92 '1i32': i32 + 88..92 '1i32': i32 + 96..98 '*x': ! + 97..98 'x': *const ! + 28..105: expected !, got () + 88..92: expected !, got i32 + "#]], + ) +} + +#[test] +fn diverging_place_match7() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn uncovered_arm() -> ! { + unsafe { + let x: *const ! = 0 as _; + let (1i32 | _) = *x; + } +} +"#, + expect![[r#" + 24..109 '{ ... } }': ! + 30..107 'unsafe... }': ! + 51..52 'x': *const ! + 65..66 '0': i32 + 65..71 '0 as _': *const ! + 86..90 '1i32': i32 + 86..90 '1i32': i32 + 86..94 '1i32 | _': ! + 93..94 '_': ! + 98..100 '*x': ! + 99..100 'x': *const ! + 30..107: expected !, got () + 86..90: expected !, got i32 + "#]], + ) +} + +#[test] +fn diverging_place_match8() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn coerce_ref_binding() -> ! { + unsafe { + let x: *const ! = 0 as _; + let ref _x: () = *x; + } +} +"#, + expect![[r#" + 29..114 '{ ... } }': ! + 35..112 'unsafe... }': ! + 56..57 'x': *const ! + 70..71 '0': i32 + 70..76 '0 as _': *const ! + 90..96 'ref _x': &'? () + 103..105 '*x': ! + 104..105 'x': *const ! + 103..105: expected (), got ! + "#]], + ) +} + +#[test] +fn never_place_isnt_diverging() { + check_infer_with_mismatches( + r#" +//- minicore: sized +fn make_up_a_pointer() -> *const T { + unsafe { + let x: *const ! = 0 as _; + &raw const *x + } +} +"#, + expect![[r#" + 38..116 '{ ... } }': *const T + 44..114 'unsafe... }': *const T + 65..66 'x': *const ! + 79..80 '0': i32 + 79..85 '0 as _': *const ! + 95..108 '&raw const *x': *const ! + 106..108 '*x': ! + 107..108 'x': *const ! + 95..108: expected *const T, got *const ! + "#]], + ) +}