diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:21 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:21 +0000 |
commit | 4e8199b572f2035b7749cba276ece3a26630d23e (patch) | |
tree | f09feeed6a0fe39d027b1908aa63ea6b35e4b631 /compiler/rustc_mir_build/src/build/expr | |
parent | Adding upstream version 1.66.0+dfsg1. (diff) | |
download | rustc-4e8199b572f2035b7749cba276ece3a26630d23e.tar.xz rustc-4e8199b572f2035b7749cba276ece3a26630d23e.zip |
Adding upstream version 1.67.1+dfsg1.upstream/1.67.1+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_mir_build/src/build/expr')
-rw-r--r-- | compiler/rustc_mir_build/src/build/expr/as_constant.rs | 163 | ||||
-rw-r--r-- | compiler/rustc_mir_build/src/build/expr/as_place.rs | 168 | ||||
-rw-r--r-- | compiler/rustc_mir_build/src/build/expr/as_rvalue.rs | 58 | ||||
-rw-r--r-- | compiler/rustc_mir_build/src/build/expr/category.rs | 28 | ||||
-rw-r--r-- | compiler/rustc_mir_build/src/build/expr/into.rs | 21 |
5 files changed, 252 insertions, 186 deletions
diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 37dc1ad9f..717c62315 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -8,7 +8,10 @@ use rustc_middle::mir::interpret::{ }; use rustc_middle::mir::*; use rustc_middle::thir::*; -use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, TyCtxt}; +use rustc_middle::ty::{ + self, CanonicalUserType, CanonicalUserTypeAnnotation, TyCtxt, UserTypeAnnotationIndex, +}; +use rustc_span::DUMMY_SP; use rustc_target::abi::Size; impl<'a, 'tcx> Builder<'a, 'tcx> { @@ -18,83 +21,87 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let this = self; let tcx = this.tcx; let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; - match *kind { + match kind { ExprKind::Scope { region_scope: _, lint_level: _, value } => { - this.as_constant(&this.thir[value]) - } - ExprKind::Literal { lit, neg } => { - let literal = - match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) { - Ok(c) => c, - Err(LitToConstError::Reported) => ConstantKind::Ty(tcx.const_error(ty)), - Err(LitToConstError::TypeError) => { - bug!("encountered type error in `lit_to_mir_constant") - } - }; - - Constant { span, user_ty: None, literal } + this.as_constant(&this.thir[*value]) } - ExprKind::NonHirLiteral { lit, ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { + _ => as_constant_inner( + expr, + |user_ty| { + Some(this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { span, user_ty: user_ty.clone(), inferred_ty: ty, - }) - }); - let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); + })) + }, + tcx, + ), + } + } +} - Constant { span, user_ty: user_ty, literal } - } - ExprKind::ZstLiteral { ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { - span, - user_ty: user_ty.clone(), - inferred_ty: ty, - }) - }); - let literal = ConstantKind::Val(ConstValue::ZeroSized, ty); +pub fn as_constant_inner<'tcx>( + expr: &Expr<'tcx>, + push_cuta: impl FnMut(&Box<CanonicalUserType<'tcx>>) -> Option<UserTypeAnnotationIndex>, + tcx: TyCtxt<'tcx>, +) -> Constant<'tcx> { + let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; + match *kind { + ExprKind::Literal { lit, neg } => { + let literal = + match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) { + Ok(c) => c, + Err(LitToConstError::Reported(guar)) => { + ConstantKind::Ty(tcx.const_error_with_guaranteed(ty, guar)) + } + Err(LitToConstError::TypeError) => { + bug!("encountered type error in `lit_to_mir_constant") + } + }; - Constant { span, user_ty: user_ty, literal } - } - ExprKind::NamedConst { def_id, substs, ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { - span, - user_ty: user_ty.clone(), - inferred_ty: ty, - }) - }); + Constant { span, user_ty: None, literal } + } + ExprKind::NonHirLiteral { lit, ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - let uneval = - mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - let literal = ConstantKind::Unevaluated(uneval, ty); + let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); - Constant { user_ty, span, literal } - } - ExprKind::ConstParam { param, def_id: _ } => { - let const_param = - tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Param(param), ty: expr.ty }); - let literal = ConstantKind::Ty(const_param); + Constant { span, user_ty: user_ty, literal } + } + ExprKind::ZstLiteral { ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - Constant { user_ty: None, span, literal } - } - ExprKind::ConstBlock { did: def_id, substs } => { - let uneval = - mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - let literal = ConstantKind::Unevaluated(uneval, ty); + let literal = ConstantKind::Val(ConstValue::ZeroSized, ty); - Constant { user_ty: None, span, literal } - } - ExprKind::StaticRef { alloc_id, ty, .. } => { - let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx)); - let literal = ConstantKind::Val(const_val, ty); + Constant { span, user_ty: user_ty, literal } + } + ExprKind::NamedConst { def_id, substs, ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - Constant { span, user_ty: None, literal } - } - _ => span_bug!(span, "expression is not a valid constant {:?}", kind), + let uneval = mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); + let literal = ConstantKind::Unevaluated(uneval, ty); + + Constant { user_ty, span, literal } } + ExprKind::ConstParam { param, def_id: _ } => { + let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty); + let literal = ConstantKind::Ty(const_param); + + Constant { user_ty: None, span, literal } + } + ExprKind::ConstBlock { did: def_id, substs } => { + let uneval = mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); + let literal = ConstantKind::Unevaluated(uneval, ty); + + Constant { user_ty: None, span, literal } + } + ExprKind::StaticRef { alloc_id, ty, .. } => { + let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx)); + let literal = ConstantKind::Val(const_val, ty); + + Constant { span, user_ty: None, literal } + } + _ => span_bug!(span, "expression is not a valid constant {:?}", kind), } } @@ -106,7 +113,15 @@ pub(crate) fn lit_to_mir_constant<'tcx>( let LitToConstInput { lit, ty, neg } = lit_input; let trunc = |n| { let param_ty = ty::ParamEnv::reveal_all().and(ty); - let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size; + let width = tcx + .layout_of(param_ty) + .map_err(|_| { + LitToConstError::Reported(tcx.sess.delay_span_bug( + DUMMY_SP, + format!("couldn't compute width of literal: {:?}", lit_input.lit), + )) + })? + .size; trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); let result = width.truncate(n); trace!("trunc result: {}", result); @@ -137,12 +152,20 @@ pub(crate) fn lit_to_mir_constant<'tcx>( (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })? } - (ast::LitKind::Float(n, _), ty::Float(fty)) => { - parse_float_into_constval(*n, *fty, neg).ok_or(LitToConstError::Reported)? - } + (ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float_into_constval(*n, *fty, neg) + .ok_or_else(|| { + LitToConstError::Reported(tcx.sess.delay_span_bug( + DUMMY_SP, + format!("couldn't parse float literal: {:?}", lit_input.lit), + )) + })?, (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)), (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)), - (ast::LitKind::Err, _) => return Err(LitToConstError::Reported), + (ast::LitKind::Err, _) => { + return Err(LitToConstError::Reported( + tcx.sess.delay_span_bug(DUMMY_SP, "encountered LitKind::Err during mir build"), + )); + } _ => return Err(LitToConstError::TypeError), }; diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 396782d45..edd527286 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -65,7 +65,7 @@ pub(crate) enum PlaceBase { /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a /// place by pushing more and more projections onto the end, and then convert the final set into a -/// place using the `into_place` method. +/// place using the `to_place` method. /// /// This is used internally when building a place for an expression like `a.b.c`. The fields `b` /// and `c` can be progressively pushed onto the place builder that is created when converting `a`. @@ -167,59 +167,54 @@ fn find_capture_matching_projections<'a, 'tcx>( }) } -/// Takes a PlaceBuilder and resolves the upvar (if any) within it, so that the -/// `PlaceBuilder` now starts from `PlaceBase::Local`. -/// -/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found. +/// Takes an upvar place and tries to resolve it into a `PlaceBuilder` +/// with `PlaceBase::Local` #[instrument(level = "trace", skip(cx), ret)] fn to_upvars_resolved_place_builder<'tcx>( - from_builder: PlaceBuilder<'tcx>, cx: &Builder<'_, 'tcx>, -) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> { - match from_builder.base { - PlaceBase::Local(_) => Ok(from_builder), - PlaceBase::Upvar { var_hir_id, closure_def_id } => { - let Some((capture_index, capture)) = - find_capture_matching_projections( - &cx.upvars, - var_hir_id, - &from_builder.projection, - ) else { - let closure_span = cx.tcx.def_span(closure_def_id); - if !enable_precise_capture(cx.tcx, closure_span) { - bug!( - "No associated capture found for {:?}[{:#?}] even though \ - capture_disjoint_fields isn't enabled", - var_hir_id, - from_builder.projection - ) - } else { - debug!( - "No associated capture found for {:?}[{:#?}]", - var_hir_id, from_builder.projection, - ); - } - return Err(from_builder); - }; + var_hir_id: LocalVarId, + closure_def_id: LocalDefId, + projection: &[PlaceElem<'tcx>], +) -> Option<PlaceBuilder<'tcx>> { + let Some((capture_index, capture)) = + find_capture_matching_projections( + &cx.upvars, + var_hir_id, + &projection, + ) else { + let closure_span = cx.tcx.def_span(closure_def_id); + if !enable_precise_capture(cx.tcx, closure_span) { + bug!( + "No associated capture found for {:?}[{:#?}] even though \ + capture_disjoint_fields isn't enabled", + var_hir_id, + projection + ) + } else { + debug!( + "No associated capture found for {:?}[{:#?}]", + var_hir_id, projection, + ); + } + return None; + }; - // Access the capture by accessing the field within the Closure struct. - let capture_info = &cx.upvars[capture_index]; + // Access the capture by accessing the field within the Closure struct. + let capture_info = &cx.upvars[capture_index]; - let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place); + let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place); - // We used some of the projections to build the capture itself, - // now we apply the remaining to the upvar resolved place. - trace!(?capture.captured_place, ?from_builder.projection); - let remaining_projections = strip_prefix( - capture.captured_place.place.base_ty, - from_builder.projection, - &capture.captured_place.place.projections, - ); - upvar_resolved_place_builder.projection.extend(remaining_projections); + // We used some of the projections to build the capture itself, + // now we apply the remaining to the upvar resolved place. + trace!(?capture.captured_place, ?projection); + let remaining_projections = strip_prefix( + capture.captured_place.place.base_ty, + projection, + &capture.captured_place.place.projections, + ); + upvar_resolved_place_builder.projection.extend(remaining_projections); - Ok(upvar_resolved_place_builder) - } - } + Some(upvar_resolved_place_builder) } /// Returns projections remaining after stripping an initial prefix of HIR @@ -228,13 +223,14 @@ fn to_upvars_resolved_place_builder<'tcx>( /// Supports only HIR projection kinds that represent a path that might be /// captured by a closure or a generator, i.e., an `Index` or a `Subslice` /// projection kinds are unsupported. -fn strip_prefix<'tcx>( +fn strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, - projections: Vec<PlaceElem<'tcx>>, + projections: &'a [PlaceElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], -) -> impl Iterator<Item = PlaceElem<'tcx>> { +) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a { let mut iter = projections - .into_iter() + .iter() + .copied() // Filter out opaque casts, they are unnecessary in the prefix. .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); for projection in prefix_projections { @@ -258,21 +254,21 @@ fn strip_prefix<'tcx>( } impl<'tcx> PlaceBuilder<'tcx> { - pub(in crate::build) fn into_place(self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> { - if let PlaceBase::Local(local) = self.base { - Place { local, projection: cx.tcx.intern_place_elems(&self.projection) } - } else { - self.expect_upvars_resolved(cx).into_place(cx) - } + pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> { + self.try_to_place(cx).unwrap() } - fn expect_upvars_resolved(self, cx: &Builder<'_, 'tcx>) -> PlaceBuilder<'tcx> { - to_upvars_resolved_place_builder(self, cx).unwrap() + /// Creates a `Place` or returns `None` if an upvar cannot be resolved + pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> { + let resolved = self.resolve_upvar(cx); + let builder = resolved.as_ref().unwrap_or(self); + let PlaceBase::Local(local) = builder.base else { return None }; + let projection = cx.tcx.intern_place_elems(&builder.projection); + Some(Place { local, projection }) } /// Attempts to resolve the `PlaceBuilder`. - /// On success, it will return the resolved `PlaceBuilder`. - /// On failure, it will return itself. + /// Returns `None` if this is not an upvar. /// /// Upvars resolve may fail for a `PlaceBuilder` when attempting to /// resolve a disjoint field whose root variable is not captured @@ -281,11 +277,14 @@ impl<'tcx> PlaceBuilder<'tcx> { /// not captured. This can happen because the final mir that will be /// generated doesn't require a read for this place. Failures will only /// happen inside closures. - pub(in crate::build) fn try_upvars_resolved( - self, + pub(in crate::build) fn resolve_upvar( + &self, cx: &Builder<'_, 'tcx>, - ) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> { - to_upvars_resolved_place_builder(self, cx) + ) -> Option<PlaceBuilder<'tcx>> { + let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else { + return None; + }; + to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection) } pub(crate) fn base(&self) -> PlaceBase { @@ -316,6 +315,14 @@ impl<'tcx> PlaceBuilder<'tcx> { self.projection.push(elem); self } + + /// Same as `.clone().project(..)` but more efficient + pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { + Self { + base: self.base, + projection: Vec::from_iter(self.projection.iter().copied().chain([elem])), + } + } } impl<'tcx> From<Local> for PlaceBuilder<'tcx> { @@ -355,7 +362,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { expr: &Expr<'tcx>, ) -> BlockAnd<Place<'tcx>> { let place_builder = unpack!(block = self.as_place_builder(block, expr)); - block.and(place_builder.into_place(self)) + block.and(place_builder.to_place(self)) } /// This is used when constructing a compound `Place`, so that we can avoid creating @@ -379,7 +386,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { expr: &Expr<'tcx>, ) -> BlockAnd<Place<'tcx>> { let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr)); - block.and(place_builder.into_place(self)) + block.and(place_builder.to_place(self)) } /// This is used when constructing a compound `Place`, so that we can avoid creating @@ -474,7 +481,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { inferred_ty: expr.ty, }); - let place = place_builder.clone().into_place(this); + let place = place_builder.to_place(this); this.cfg.push( block, Statement { @@ -599,7 +606,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let is_outermost_index = fake_borrow_temps.is_none(); let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps); - let mut base_place = + let base_place = unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),)); // Making this a *fresh* temporary means we do not have to worry about @@ -607,14 +614,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // The "retagging" transformation (for Stacked Borrows) relies on this. let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,)); - block = self.bounds_check(block, base_place.clone(), idx, expr_span, source_info); + block = self.bounds_check(block, &base_place, idx, expr_span, source_info); if is_outermost_index { self.read_fake_borrows(block, fake_borrow_temps, source_info) } else { - base_place = base_place.expect_upvars_resolved(self); self.add_fake_borrows_of_base( - &base_place, + base_place.to_place(self), block, fake_borrow_temps, expr_span, @@ -628,7 +634,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { fn bounds_check( &mut self, block: BasicBlock, - slice: PlaceBuilder<'tcx>, + slice: &PlaceBuilder<'tcx>, index: Local, expr_span: Span, source_info: SourceInfo, @@ -640,7 +646,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let lt = self.temp(bool_ty, expr_span); // len = len(slice) - self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.into_place(self))); + self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self))); // lt = idx < len self.cfg.push_assign( block, @@ -658,19 +664,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { fn add_fake_borrows_of_base( &mut self, - base_place: &PlaceBuilder<'tcx>, + base_place: Place<'tcx>, block: BasicBlock, fake_borrow_temps: &mut Vec<Local>, expr_span: Span, source_info: SourceInfo, ) { let tcx = self.tcx; - let local = match base_place.base { - PlaceBase::Local(local) => local, - PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"), - }; - let place_ty = Place::ty_from(local, &base_place.projection, &self.local_decls, tcx); + let place_ty = base_place.ty(&self.local_decls, tcx); if let ty::Slice(_) = place_ty.ty.kind() { // We need to create fake borrows to ensure that the bounds // check that we just did stays valid. Since we can't assign to @@ -680,7 +682,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { match elem { ProjectionElem::Deref => { let fake_borrow_deref_ty = Place::ty_from( - local, + base_place.local, &base_place.projection[..idx], &self.local_decls, tcx, @@ -698,14 +700,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Rvalue::Ref( tcx.lifetimes.re_erased, BorrowKind::Shallow, - Place { local, projection }, + Place { local: base_place.local, projection }, ), ); fake_borrow_temps.push(fake_borrow_temp); } ProjectionElem::Index(_) => { let index_ty = Place::ty_from( - local, + base_place.local, &base_place.projection[..idx], &self.local_decls, tcx, diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 3dafdcb78..0814793f2 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -2,6 +2,7 @@ use rustc_index::vec::Idx; use rustc_middle::ty::util::IntTypeExt; +use rustc_target::abi::{Abi, Primitive}; use crate::build::expr::as_place::PlaceBase; use crate::build::expr::category::{Category, RvalueFunc}; @@ -198,6 +199,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let (source, ty) = if let ty::Adt(adt_def, ..) = source.ty.kind() && adt_def.is_enum() { let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx); let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not)); + let layout = this.tcx.layout_of(this.param_env.and(source.ty)); let discr = this.temp(discr_ty, source.span); this.cfg.push_assign( block, @@ -205,8 +207,55 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { discr, Rvalue::Discriminant(temp.into()), ); + let (op,ty) = (Operand::Move(discr), discr_ty); + + if let Abi::Scalar(scalar) = layout.unwrap().abi{ + if let Primitive::Int(_, signed) = scalar.primitive() { + let range = scalar.valid_range(&this.tcx); + // FIXME: Handle wraparound cases too. + if range.end >= range.start { + let mut assumer = |range: u128, bin_op: BinOp| { + // We will be overwriting this val if our scalar is signed value + // because sign extension on unsigned types might cause unintended things + let mut range_val = + ConstantKind::from_bits(this.tcx, range, ty::ParamEnv::empty().and(discr_ty)); + let bool_ty = this.tcx.types.bool; + if signed { + let scalar_size_extend = scalar.size(&this.tcx).sign_extend(range); + let discr_layout = this.tcx.layout_of(this.param_env.and(discr_ty)); + let truncated_val = discr_layout.unwrap().size.truncate(scalar_size_extend); + range_val = ConstantKind::from_bits( + this.tcx, + truncated_val, + ty::ParamEnv::empty().and(discr_ty), + ); + } + let lit_op = this.literal_operand(expr.span, range_val); + let is_bin_op = this.temp(bool_ty, expr_span); + this.cfg.push_assign( + block, + source_info, + is_bin_op, + Rvalue::BinaryOp(bin_op, Box::new(((lit_op), (Operand::Copy(discr))))), + ); + this.cfg.push( + block, + Statement { + source_info, + kind: StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume( + Operand::Copy(is_bin_op), + ))), + }, + ) + }; + assumer(range.end, BinOp::Ge); + assumer(range.start, BinOp::Le); + } + } + } + + (op,ty) - (Operand::Move(discr), discr_ty) } else { let ty = source.ty; let source = unpack!( @@ -320,8 +369,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let place_builder = unpack!(block = this.as_place_builder(block, &this.thir[*thir_place])); - if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) { - let mir_place = place_builder_resolved.into_place(this); + if let Some(mir_place) = place_builder.try_to_place(this) { this.cfg.push_fake_read( block, this.source_info(this.tcx.hir().span(*hir_id)), @@ -612,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // by the parent itself. The mutability of the current capture // is same as that of the capture in the parent closure. PlaceBase::Upvar { .. } => { - let enclosing_upvars_resolved = arg_place_builder.clone().into_place(this); + let enclosing_upvars_resolved = arg_place_builder.to_place(this); match enclosing_upvars_resolved.as_ref() { PlaceRef { @@ -649,7 +697,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false }, }; - let arg_place = arg_place_builder.into_place(this); + let arg_place = arg_place_builder.to_place(this); this.cfg.push_assign( block, diff --git a/compiler/rustc_mir_build/src/build/expr/category.rs b/compiler/rustc_mir_build/src/build/expr/category.rs index a4386319d..d33401f07 100644 --- a/compiler/rustc_mir_build/src/build/expr/category.rs +++ b/compiler/rustc_mir_build/src/build/expr/category.rs @@ -2,35 +2,35 @@ use rustc_middle::thir::*; #[derive(Debug, PartialEq)] pub(crate) enum Category { - // An assignable memory location like `x`, `x.f`, `foo()[3]`, that - // sort of thing. Something that could appear on the LHS of an `=` - // sign. + /// An assignable memory location like `x`, `x.f`, `foo()[3]`, that + /// sort of thing. Something that could appear on the LHS of an `=` + /// sign. Place, - // A literal like `23` or `"foo"`. Does not include constant - // expressions like `3 + 5`. + /// A literal like `23` or `"foo"`. Does not include constant + /// expressions like `3 + 5`. Constant, - // Something that generates a new value at runtime, like `x + y` - // or `foo()`. + /// Something that generates a new value at runtime, like `x + y` + /// or `foo()`. Rvalue(RvalueFunc), } -// Rvalues fall into different "styles" that will determine which fn -// is best suited to generate them. +/// Rvalues fall into different "styles" that will determine which fn +/// is best suited to generate them. #[derive(Debug, PartialEq)] pub(crate) enum RvalueFunc { - // Best generated by `into`. This is generally exprs that - // cause branching, like `match`, but also includes calls. + /// Best generated by `into`. This is generally exprs that + /// cause branching, like `match`, but also includes calls. Into, - // Best generated by `as_rvalue`. This is usually the case. + /// Best generated by `as_rvalue`. This is usually the case. AsRvalue, } -/// Determines the category for a given expression. Note that scope -/// and paren expressions have no category. impl Category { + /// Determines the category for a given expression. Note that scope + /// and paren expressions have no category. pub(crate) fn of(ek: &ExprKind<'_>) -> Option<Category> { match *ek { ExprKind::Scope { .. } => None, diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 24ecd0a53..218a26e62 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -108,7 +108,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ExprKind::Let { expr, ref pat } => { let scope = this.local_scope(); let (true_block, false_block) = this.in_if_then_scope(scope, expr_span, |this| { - this.lower_let_expr(block, &this.thir[expr], pat, scope, None, expr_span) + this.lower_let_expr(block, &this.thir[expr], pat, scope, None, expr_span, true) }); this.cfg.push_assign_constant( @@ -271,15 +271,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // MIR checks and ultimately whether code is accepted or not. We can only // omit the return edge if a return type is visibly uninhabited to a module // that makes the call. - target: if this.tcx.is_ty_uninhabited_from( - this.parent_module, - expr.ty, - this.param_env, - ) { - None - } else { - Some(success) - }, + target: expr + .ty + .is_inhabited_from(this.tcx, this.parent_module, this.param_env) + .then_some(success), from_hir_call, fn_span, }, @@ -363,10 +358,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .map(|(n, ty)| match fields_map.get(&n) { Some(v) => v.clone(), None => { - let place_builder = place_builder.clone(); - this.consume_by_copy_or_move( - place_builder.field(n, *ty).into_place(this), - ) + let place = place_builder.clone_project(PlaceElem::Field(n, *ty)); + this.consume_by_copy_or_move(place.to_place(this)) } }) .collect() |