Auto merge of #94876 - b-naber:thir-abstract-const-changes, r=lcnr
Change Thir to lazily create constants To allow `AbstractConst`s to work with the previous thir changes we made and those we want to make, i.e. to avoid problems due to `ValTree` and `ConstValue` conversions, we instead switch to a thir representation for constants that allows us to lazily create constants. r? `@oli-obk`
This commit is contained in:
commit
8d8135f003
17 changed files with 345 additions and 154 deletions
|
@ -955,6 +955,7 @@ rustc_queries! {
|
||||||
desc { "get a &core::panic::Location referring to a span" }
|
desc { "get a &core::panic::Location referring to a span" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME get rid of this with valtrees
|
||||||
query lit_to_const(
|
query lit_to_const(
|
||||||
key: LitToConstInput<'tcx>
|
key: LitToConstInput<'tcx>
|
||||||
) -> Result<ty::Const<'tcx>, LitToConstError> {
|
) -> Result<ty::Const<'tcx>, LitToConstError> {
|
||||||
|
|
|
@ -369,7 +369,8 @@ pub enum ExprKind<'tcx> {
|
||||||
},
|
},
|
||||||
/// An inline `const` block, e.g. `const {}`.
|
/// An inline `const` block, e.g. `const {}`.
|
||||||
ConstBlock {
|
ConstBlock {
|
||||||
value: Const<'tcx>,
|
did: DefId,
|
||||||
|
substs: SubstsRef<'tcx>,
|
||||||
},
|
},
|
||||||
/// An array literal constructed from one repeated element, e.g. `[1; 5]`.
|
/// An array literal constructed from one repeated element, e.g. `[1; 5]`.
|
||||||
Repeat {
|
Repeat {
|
||||||
|
@ -408,13 +409,25 @@ pub enum ExprKind<'tcx> {
|
||||||
},
|
},
|
||||||
/// A literal.
|
/// A literal.
|
||||||
Literal {
|
Literal {
|
||||||
literal: Const<'tcx>,
|
lit: &'tcx hir::Lit,
|
||||||
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
|
neg: bool,
|
||||||
/// The `DefId` of the `const` item this literal
|
|
||||||
/// was produced from, if this is not a user-written
|
|
||||||
/// literal value.
|
|
||||||
const_id: Option<DefId>,
|
|
||||||
},
|
},
|
||||||
|
/// For literals that don't correspond to anything in the HIR
|
||||||
|
NonHirLiteral {
|
||||||
|
lit: ty::ScalarInt,
|
||||||
|
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
|
||||||
|
},
|
||||||
|
/// Associated constants and named constants
|
||||||
|
NamedConst {
|
||||||
|
def_id: DefId,
|
||||||
|
substs: SubstsRef<'tcx>,
|
||||||
|
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
|
||||||
|
},
|
||||||
|
ConstParam {
|
||||||
|
param: ty::ParamConst,
|
||||||
|
def_id: DefId,
|
||||||
|
},
|
||||||
|
// FIXME improve docs for `StaticRef` by distinguishing it from `NamedConst`
|
||||||
/// A literal containing the address of a `static`.
|
/// A literal containing the address of a `static`.
|
||||||
///
|
///
|
||||||
/// This is only distinguished from `Literal` so that we can register some
|
/// This is only distinguished from `Literal` so that we can register some
|
||||||
|
@ -439,6 +452,12 @@ pub enum ExprKind<'tcx> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'tcx> ExprKind<'tcx> {
|
||||||
|
pub fn zero_sized_literal(user_ty: Option<Canonical<'tcx, UserType<'tcx>>>) -> Self {
|
||||||
|
ExprKind::NonHirLiteral { lit: ty::ScalarInt::ZST, user_ty }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Represents the association of a field identifier and an expression.
|
/// Represents the association of a field identifier and an expression.
|
||||||
///
|
///
|
||||||
/// This is used in struct constructors.
|
/// This is used in struct constructors.
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use super::{
|
use super::{
|
||||||
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
|
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::Const;
|
|
||||||
|
|
||||||
pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
||||||
fn thir(&self) -> &'a Thir<'tcx>;
|
fn thir(&self) -> &'a Thir<'tcx>;
|
||||||
|
@ -25,8 +24,6 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
||||||
fn visit_pat(&mut self, pat: &Pat<'tcx>) {
|
fn visit_pat(&mut self, pat: &Pat<'tcx>) {
|
||||||
walk_pat(self, pat);
|
walk_pat(self, pat);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_const(&mut self, _cnst: Const<'tcx>) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) {
|
pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) {
|
||||||
|
@ -93,10 +90,9 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
|
||||||
visitor.visit_expr(&visitor.thir()[value])
|
visitor.visit_expr(&visitor.thir()[value])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ConstBlock { value } => visitor.visit_const(value),
|
ConstBlock { did: _, substs: _ } => {}
|
||||||
Repeat { value, count } => {
|
Repeat { value, count: _ } => {
|
||||||
visitor.visit_expr(&visitor.thir()[value]);
|
visitor.visit_expr(&visitor.thir()[value]);
|
||||||
visitor.visit_const(count);
|
|
||||||
}
|
}
|
||||||
Array { ref fields } | Tuple { ref fields } => {
|
Array { ref fields } | Tuple { ref fields } => {
|
||||||
for &field in &**fields {
|
for &field in &**fields {
|
||||||
|
@ -122,7 +118,10 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
|
||||||
visitor.visit_expr(&visitor.thir()[source])
|
visitor.visit_expr(&visitor.thir()[source])
|
||||||
}
|
}
|
||||||
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
|
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
|
||||||
Literal { literal, user_ty: _, const_id: _ } => visitor.visit_const(literal),
|
Literal { lit: _, neg: _ } => {}
|
||||||
|
NonHirLiteral { lit: _, user_ty: _ } => {}
|
||||||
|
NamedConst { def_id: _, substs: _, user_ty: _ } => {}
|
||||||
|
ConstParam { param: _, def_id: _ } => {}
|
||||||
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
|
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
|
||||||
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
|
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
|
||||||
for op in &**operands {
|
for op in &**operands {
|
||||||
|
@ -209,11 +208,8 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'
|
||||||
visitor.visit_pat(&subpattern.pattern);
|
visitor.visit_pat(&subpattern.pattern);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Constant { value } => visitor.visit_const(*value),
|
Constant { value: _ } => {}
|
||||||
Range(range) => {
|
Range(_) => {}
|
||||||
visitor.visit_const(range.lo);
|
|
||||||
visitor.visit_const(range.hi);
|
|
||||||
}
|
|
||||||
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
|
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
|
||||||
for subpattern in prefix {
|
for subpattern in prefix {
|
||||||
visitor.visit_pat(&subpattern);
|
visitor.visit_pat(&subpattern);
|
||||||
|
|
|
@ -1,22 +1,48 @@
|
||||||
//! See docs in build/expr/mod.rs
|
//! See docs in build/expr/mod.rs
|
||||||
|
|
||||||
use crate::build::Builder;
|
use crate::build::Builder;
|
||||||
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
use crate::thir::constant::parse_float;
|
||||||
|
use rustc_ast::ast;
|
||||||
|
use rustc_hir::def_id::DefId;
|
||||||
|
use rustc_middle::mir::interpret::{
|
||||||
|
Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
|
||||||
|
};
|
||||||
use rustc_middle::mir::*;
|
use rustc_middle::mir::*;
|
||||||
use rustc_middle::thir::*;
|
use rustc_middle::thir::*;
|
||||||
use rustc_middle::ty::CanonicalUserTypeAnnotation;
|
use rustc_middle::ty::subst::SubstsRef;
|
||||||
|
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt};
|
||||||
|
use rustc_target::abi::Size;
|
||||||
|
|
||||||
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
/// Compile `expr`, yielding a compile-time constant. Assumes that
|
/// Compile `expr`, yielding a compile-time constant. Assumes that
|
||||||
/// `expr` is a valid compile-time constant!
|
/// `expr` is a valid compile-time constant!
|
||||||
crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
|
crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
|
||||||
|
let create_uneval_from_def_id =
|
||||||
|
|tcx: TyCtxt<'tcx>, def_id: DefId, ty: Ty<'tcx>, substs: SubstsRef<'tcx>| {
|
||||||
|
let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
|
||||||
|
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Unevaluated(uneval), ty })
|
||||||
|
};
|
||||||
|
|
||||||
let this = self;
|
let this = self;
|
||||||
|
let tcx = this.tcx;
|
||||||
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
|
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
|
||||||
match *kind {
|
match *kind {
|
||||||
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
|
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
|
||||||
this.as_constant(&this.thir[value])
|
this.as_constant(&this.thir[value])
|
||||||
}
|
}
|
||||||
ExprKind::Literal { literal, user_ty, const_id: _ } => {
|
ExprKind::Literal { lit, neg } => {
|
||||||
|
let literal =
|
||||||
|
match lit_to_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_constant")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Constant { span, user_ty: None, literal: literal.into() }
|
||||||
|
}
|
||||||
|
ExprKind::NonHirLiteral { lit, user_ty } => {
|
||||||
let user_ty = user_ty.map(|user_ty| {
|
let user_ty = user_ty.map(|user_ty| {
|
||||||
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
||||||
span,
|
span,
|
||||||
|
@ -24,20 +50,92 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
inferred_ty: ty,
|
inferred_ty: ty,
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
assert_eq!(literal.ty(), ty);
|
|
||||||
Constant { span, user_ty, literal: literal.into() }
|
let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
|
||||||
|
|
||||||
|
Constant { span, user_ty: user_ty, literal }
|
||||||
|
}
|
||||||
|
ExprKind::NamedConst { def_id, substs, user_ty } => {
|
||||||
|
let user_ty = user_ty.map(|user_ty| {
|
||||||
|
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
|
||||||
|
span,
|
||||||
|
user_ty,
|
||||||
|
inferred_ty: ty,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs));
|
||||||
|
|
||||||
|
Constant { user_ty, span, literal }
|
||||||
|
}
|
||||||
|
ExprKind::ConstParam { param, def_id: _ } => {
|
||||||
|
let const_param =
|
||||||
|
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Param(param), ty: expr.ty });
|
||||||
|
let literal = ConstantKind::Ty(const_param);
|
||||||
|
|
||||||
|
Constant { user_ty: None, span, literal }
|
||||||
|
}
|
||||||
|
ExprKind::ConstBlock { did: def_id, substs } => {
|
||||||
|
let literal = ConstantKind::Ty(create_uneval_from_def_id(tcx, def_id, ty, substs));
|
||||||
|
|
||||||
|
Constant { user_ty: None, span, literal }
|
||||||
}
|
}
|
||||||
ExprKind::StaticRef { alloc_id, ty, .. } => {
|
ExprKind::StaticRef { alloc_id, ty, .. } => {
|
||||||
let const_val =
|
let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx));
|
||||||
ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx));
|
|
||||||
let literal = ConstantKind::Val(const_val, ty);
|
let literal = ConstantKind::Val(const_val, ty);
|
||||||
|
|
||||||
Constant { span, user_ty: None, literal }
|
Constant { span, user_ty: None, literal }
|
||||||
}
|
}
|
||||||
ExprKind::ConstBlock { value } => {
|
|
||||||
Constant { span: span, user_ty: None, literal: value.into() }
|
|
||||||
}
|
|
||||||
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
|
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
crate fn lit_to_constant<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
|
lit_input: LitToConstInput<'tcx>,
|
||||||
|
) -> Result<ConstantKind<'tcx>, LitToConstError> {
|
||||||
|
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;
|
||||||
|
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
|
||||||
|
let result = width.truncate(n);
|
||||||
|
trace!("trunc result: {}", result);
|
||||||
|
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
|
||||||
|
};
|
||||||
|
|
||||||
|
let value = match (lit, &ty.kind()) {
|
||||||
|
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
|
||||||
|
let s = s.as_str();
|
||||||
|
let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
|
||||||
|
let allocation = tcx.intern_const_alloc(allocation);
|
||||||
|
ConstValue::Slice { data: allocation, start: 0, end: s.len() }
|
||||||
|
}
|
||||||
|
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _))
|
||||||
|
if matches!(inner_ty.kind(), ty::Slice(_)) =>
|
||||||
|
{
|
||||||
|
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
|
||||||
|
let allocation = tcx.intern_const_alloc(allocation);
|
||||||
|
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
|
||||||
|
}
|
||||||
|
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
|
||||||
|
let id = tcx.allocate_bytes(data);
|
||||||
|
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
|
||||||
|
}
|
||||||
|
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
|
||||||
|
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
|
||||||
|
}
|
||||||
|
(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(*n, *fty, neg).ok_or(LitToConstError::Reported)?
|
||||||
|
}
|
||||||
|
(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),
|
||||||
|
_ => return Err(LitToConstError::TypeError),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(ConstantKind::Val(value, ty))
|
||||||
|
}
|
||||||
|
|
|
@ -566,6 +566,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
| ExprKind::Continue { .. }
|
| ExprKind::Continue { .. }
|
||||||
| ExprKind::Return { .. }
|
| ExprKind::Return { .. }
|
||||||
| ExprKind::Literal { .. }
|
| ExprKind::Literal { .. }
|
||||||
|
| ExprKind::NamedConst { .. }
|
||||||
|
| ExprKind::NonHirLiteral { .. }
|
||||||
|
| ExprKind::ConstParam { .. }
|
||||||
| ExprKind::ConstBlock { .. }
|
| ExprKind::ConstBlock { .. }
|
||||||
| ExprKind::StaticRef { .. }
|
| ExprKind::StaticRef { .. }
|
||||||
| ExprKind::InlineAsm { .. }
|
| ExprKind::InlineAsm { .. }
|
||||||
|
|
|
@ -327,6 +327,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
ExprKind::Yield { .. }
|
ExprKind::Yield { .. }
|
||||||
| ExprKind::Literal { .. }
|
| ExprKind::Literal { .. }
|
||||||
|
| ExprKind::NamedConst { .. }
|
||||||
|
| ExprKind::NonHirLiteral { .. }
|
||||||
|
| ExprKind::ConstParam { .. }
|
||||||
| ExprKind::ConstBlock { .. }
|
| ExprKind::ConstBlock { .. }
|
||||||
| ExprKind::StaticRef { .. }
|
| ExprKind::StaticRef { .. }
|
||||||
| ExprKind::Block { .. }
|
| ExprKind::Block { .. }
|
||||||
|
|
|
@ -70,7 +70,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
local_decl.local_info =
|
local_decl.local_info =
|
||||||
Some(Box::new(LocalInfo::StaticRef { def_id, is_thread_local: true }));
|
Some(Box::new(LocalInfo::StaticRef { def_id, is_thread_local: true }));
|
||||||
}
|
}
|
||||||
ExprKind::Literal { const_id: Some(def_id), .. } => {
|
ExprKind::NamedConst { def_id, .. } | ExprKind::ConstParam { def_id, .. } => {
|
||||||
local_decl.local_info = Some(Box::new(LocalInfo::ConstRef { def_id }));
|
local_decl.local_info = Some(Box::new(LocalInfo::ConstRef { def_id }));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -69,9 +69,12 @@ impl Category {
|
||||||
| ExprKind::AssignOp { .. }
|
| ExprKind::AssignOp { .. }
|
||||||
| ExprKind::ThreadLocalRef(_) => Some(Category::Rvalue(RvalueFunc::AsRvalue)),
|
| ExprKind::ThreadLocalRef(_) => Some(Category::Rvalue(RvalueFunc::AsRvalue)),
|
||||||
|
|
||||||
ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } | ExprKind::StaticRef { .. } => {
|
ExprKind::ConstBlock { .. }
|
||||||
Some(Category::Constant)
|
| ExprKind::Literal { .. }
|
||||||
}
|
| ExprKind::NonHirLiteral { .. }
|
||||||
|
| ExprKind::ConstParam { .. }
|
||||||
|
| ExprKind::StaticRef { .. }
|
||||||
|
| ExprKind::NamedConst { .. } => Some(Category::Constant),
|
||||||
|
|
||||||
ExprKind::Loop { .. }
|
ExprKind::Loop { .. }
|
||||||
| ExprKind::Block { .. }
|
| ExprKind::Block { .. }
|
||||||
|
|
|
@ -533,6 +533,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
| ExprKind::Closure { .. }
|
| ExprKind::Closure { .. }
|
||||||
| ExprKind::ConstBlock { .. }
|
| ExprKind::ConstBlock { .. }
|
||||||
| ExprKind::Literal { .. }
|
| ExprKind::Literal { .. }
|
||||||
|
| ExprKind::NamedConst { .. }
|
||||||
|
| ExprKind::NonHirLiteral { .. }
|
||||||
|
| ExprKind::ConstParam { .. }
|
||||||
| ExprKind::ThreadLocalRef(_)
|
| ExprKind::ThreadLocalRef(_)
|
||||||
| ExprKind::StaticRef { .. } => {
|
| ExprKind::StaticRef { .. } => {
|
||||||
debug_assert!(match Category::of(&expr.kind).unwrap() {
|
debug_assert!(match Category::of(&expr.kind).unwrap() {
|
||||||
|
|
|
@ -60,7 +60,7 @@
|
||||||
//! basically the point where the "by value" operations are bridged
|
//! basically the point where the "by value" operations are bridged
|
||||||
//! over to the "by reference" mode (`as_place`).
|
//! over to the "by reference" mode (`as_place`).
|
||||||
|
|
||||||
mod as_constant;
|
crate mod as_constant;
|
||||||
mod as_operand;
|
mod as_operand;
|
||||||
pub mod as_place;
|
pub mod as_place;
|
||||||
mod as_rvalue;
|
mod as_rvalue;
|
||||||
|
|
|
@ -303,6 +303,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
|
||||||
| ExprKind::Block { .. }
|
| ExprKind::Block { .. }
|
||||||
| ExprKind::Borrow { .. }
|
| ExprKind::Borrow { .. }
|
||||||
| ExprKind::Literal { .. }
|
| ExprKind::Literal { .. }
|
||||||
|
| ExprKind::NamedConst { .. }
|
||||||
|
| ExprKind::NonHirLiteral { .. }
|
||||||
|
| ExprKind::ConstParam { .. }
|
||||||
| ExprKind::ConstBlock { .. }
|
| ExprKind::ConstBlock { .. }
|
||||||
| ExprKind::Deref { .. }
|
| ExprKind::Deref { .. }
|
||||||
| ExprKind::Index { .. }
|
| ExprKind::Index { .. }
|
||||||
|
|
|
@ -7,6 +7,7 @@ use rustc_middle::ty::{self, ParamEnv, TyCtxt};
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::Symbol;
|
||||||
use rustc_target::abi::Size;
|
use rustc_target::abi::Size;
|
||||||
|
|
||||||
|
// FIXME Once valtrees are available, get rid of this function and the query
|
||||||
crate fn lit_to_const<'tcx>(
|
crate fn lit_to_const<'tcx>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
lit_input: LitToConstInput<'tcx>,
|
lit_input: LitToConstInput<'tcx>,
|
||||||
|
@ -57,7 +58,12 @@ crate fn lit_to_const<'tcx>(
|
||||||
Ok(ty::Const::from_value(tcx, lit, ty))
|
Ok(ty::Const::from_value(tcx, lit, ty))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Option<ConstValue<'tcx>> {
|
// FIXME move this to rustc_mir_build::build
|
||||||
|
pub(crate) fn parse_float<'tcx>(
|
||||||
|
num: Symbol,
|
||||||
|
fty: ty::FloatTy,
|
||||||
|
neg: bool,
|
||||||
|
) -> Option<ConstValue<'tcx>> {
|
||||||
let num = num.as_str();
|
let num = num.as_str();
|
||||||
use rustc_apfloat::ieee::{Double, Single};
|
use rustc_apfloat::ieee::{Double, Single};
|
||||||
let scalar = match fty {
|
let scalar = match fty {
|
||||||
|
|
|
@ -14,7 +14,9 @@ use rustc_middle::ty::adjustment::{
|
||||||
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
|
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
|
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
|
||||||
use rustc_middle::ty::{self, AdtKind, Ty, UpvarSubsts, UserType};
|
use rustc_middle::ty::{
|
||||||
|
self, AdtKind, InlineConstSubsts, InlineConstSubstsParts, ScalarInt, Ty, UpvarSubsts, UserType,
|
||||||
|
};
|
||||||
use rustc_span::def_id::DefId;
|
use rustc_span::def_id::DefId;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use rustc_target::abi::VariantIdx;
|
use rustc_target::abi::VariantIdx;
|
||||||
|
@ -290,11 +292,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
|
hir::ExprKind::Lit(ref lit) => ExprKind::Literal { lit, neg: false },
|
||||||
literal: self.const_eval_literal(&lit.node, expr_ty, lit.span, false),
|
|
||||||
user_ty: None,
|
|
||||||
const_id: None,
|
|
||||||
},
|
|
||||||
|
|
||||||
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
||||||
if self.typeck_results().is_method_call(expr) {
|
if self.typeck_results().is_method_call(expr) {
|
||||||
|
@ -359,11 +357,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
let arg = self.mirror_expr(arg);
|
let arg = self.mirror_expr(arg);
|
||||||
self.overloaded_operator(expr, Box::new([arg]))
|
self.overloaded_operator(expr, Box::new([arg]))
|
||||||
} else if let hir::ExprKind::Lit(ref lit) = arg.kind {
|
} else if let hir::ExprKind::Lit(ref lit) = arg.kind {
|
||||||
ExprKind::Literal {
|
ExprKind::Literal { lit, neg: true }
|
||||||
literal: self.const_eval_literal(&lit.node, expr_ty, lit.span, true),
|
|
||||||
user_ty: None,
|
|
||||||
const_id: None,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
ExprKind::Unary { op: UnOp::Neg, arg: self.mirror_expr(arg) }
|
ExprKind::Unary { op: UnOp::Neg, arg: self.mirror_expr(arg) }
|
||||||
}
|
}
|
||||||
|
@ -524,11 +518,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
ty,
|
ty,
|
||||||
temp_lifetime,
|
temp_lifetime,
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
kind: ExprKind::Literal {
|
kind: ExprKind::zero_sized_literal(user_ty),
|
||||||
literal: ty::Const::zero_sized(self.tcx, ty),
|
|
||||||
user_ty,
|
|
||||||
const_id: None,
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -550,11 +540,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
ty,
|
ty,
|
||||||
temp_lifetime,
|
temp_lifetime,
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
kind: ExprKind::Literal {
|
kind: ExprKind::zero_sized_literal(None),
|
||||||
literal: ty::Const::zero_sized(self.tcx, ty),
|
|
||||||
user_ty: None,
|
|
||||||
const_id: None,
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -568,13 +554,21 @@ impl<'tcx> Cx<'tcx> {
|
||||||
},
|
},
|
||||||
|
|
||||||
hir::ExprKind::ConstBlock(ref anon_const) => {
|
hir::ExprKind::ConstBlock(ref anon_const) => {
|
||||||
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
|
let tcx = self.tcx;
|
||||||
|
let local_def_id = tcx.hir().local_def_id(anon_const.hir_id);
|
||||||
|
let anon_const_def_id = local_def_id.to_def_id();
|
||||||
|
|
||||||
// FIXME Do we want to use `from_inline_const` once valtrees
|
// Need to include the parent substs
|
||||||
// are introduced? This would create `ValTree`s that will never be used...
|
let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
|
||||||
let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
|
let ty = tcx.typeck(local_def_id).node_type(hir_id);
|
||||||
|
let typeck_root_def_id = tcx.typeck_root_def_id(anon_const_def_id);
|
||||||
|
let parent_substs =
|
||||||
|
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
|
||||||
|
let substs =
|
||||||
|
InlineConstSubsts::new(tcx, InlineConstSubstsParts { parent_substs, ty })
|
||||||
|
.substs;
|
||||||
|
|
||||||
ExprKind::ConstBlock { value }
|
ExprKind::ConstBlock { did: anon_const_def_id, substs }
|
||||||
}
|
}
|
||||||
// Now comes the rote stuff:
|
// Now comes the rote stuff:
|
||||||
hir::ExprKind::Repeat(ref v, _) => {
|
hir::ExprKind::Repeat(ref v, _) => {
|
||||||
|
@ -692,32 +686,36 @@ impl<'tcx> Cx<'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let source = if let Some((did, offset, var_ty)) = var {
|
let source = if let Some((did, offset, var_ty)) = var {
|
||||||
let mk_const = |literal| Expr {
|
let param_env_ty = self.param_env.and(var_ty);
|
||||||
|
let size = self
|
||||||
|
.tcx
|
||||||
|
.layout_of(param_env_ty)
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
|
||||||
|
})
|
||||||
|
.size;
|
||||||
|
let lit = ScalarInt::try_from_uint(offset as u128, size).unwrap();
|
||||||
|
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
|
||||||
|
let offset = self.thir.exprs.push(Expr {
|
||||||
temp_lifetime,
|
temp_lifetime,
|
||||||
ty: var_ty,
|
ty: var_ty,
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
kind: ExprKind::Literal { literal, user_ty: None, const_id: None },
|
kind,
|
||||||
};
|
});
|
||||||
let offset = self.thir.exprs.push(mk_const(ty::Const::from_bits(
|
|
||||||
self.tcx,
|
|
||||||
offset as u128,
|
|
||||||
self.param_env.and(var_ty),
|
|
||||||
)));
|
|
||||||
match did {
|
match did {
|
||||||
Some(did) => {
|
Some(did) => {
|
||||||
// in case we are offsetting from a computed discriminant
|
// in case we are offsetting from a computed discriminant
|
||||||
// and not the beginning of discriminants (which is always `0`)
|
// and not the beginning of discriminants (which is always `0`)
|
||||||
let substs = InternalSubsts::identity_for_item(self.tcx(), did);
|
let substs = InternalSubsts::identity_for_item(self.tcx(), did);
|
||||||
let lhs = ty::ConstS {
|
let kind =
|
||||||
val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(
|
ExprKind::NamedConst { def_id: did, substs, user_ty: None };
|
||||||
ty::WithOptConstParam::unknown(did),
|
let lhs = self.thir.exprs.push(Expr {
|
||||||
substs,
|
temp_lifetime,
|
||||||
)),
|
|
||||||
ty: var_ty,
|
ty: var_ty,
|
||||||
};
|
span: expr.span,
|
||||||
let lhs = self.thir.exprs.push(mk_const(self.tcx().mk_const(lhs)));
|
kind,
|
||||||
let bin =
|
});
|
||||||
ExprKind::Binary { op: BinOp::Add, lhs: lhs, rhs: offset };
|
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
|
||||||
self.thir.exprs.push(Expr {
|
self.thir.exprs.push(Expr {
|
||||||
temp_lifetime,
|
temp_lifetime,
|
||||||
ty: var_ty,
|
ty: var_ty,
|
||||||
|
@ -832,16 +830,7 @@ impl<'tcx> Cx<'tcx> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let ty = self.tcx().mk_fn_def(def_id, substs);
|
let ty = self.tcx().mk_fn_def(def_id, substs);
|
||||||
Expr {
|
Expr { temp_lifetime, ty, span, kind: ExprKind::zero_sized_literal(user_ty) }
|
||||||
temp_lifetime,
|
|
||||||
ty,
|
|
||||||
span,
|
|
||||||
kind: ExprKind::Literal {
|
|
||||||
literal: ty::Const::zero_sized(self.tcx(), ty),
|
|
||||||
user_ty,
|
|
||||||
const_id: None,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
|
fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
|
||||||
|
@ -868,17 +857,9 @@ impl<'tcx> Cx<'tcx> {
|
||||||
Res::Def(DefKind::Fn, _)
|
Res::Def(DefKind::Fn, _)
|
||||||
| Res::Def(DefKind::AssocFn, _)
|
| Res::Def(DefKind::AssocFn, _)
|
||||||
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
|
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
|
||||||
| Res::SelfCtor(..) => {
|
| Res::SelfCtor(_) => {
|
||||||
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
|
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
|
||||||
debug!("convert_path_expr: user_ty={:?}", user_ty);
|
ExprKind::zero_sized_literal(user_ty)
|
||||||
ExprKind::Literal {
|
|
||||||
literal: ty::Const::zero_sized(
|
|
||||||
self.tcx,
|
|
||||||
self.typeck_results().node_type(expr.hir_id),
|
|
||||||
),
|
|
||||||
user_ty,
|
|
||||||
const_id: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Res::Def(DefKind::ConstParam, def_id) => {
|
Res::Def(DefKind::ConstParam, def_id) => {
|
||||||
|
@ -888,31 +869,14 @@ impl<'tcx> Cx<'tcx> {
|
||||||
let generics = self.tcx.generics_of(item_def_id);
|
let generics = self.tcx.generics_of(item_def_id);
|
||||||
let index = generics.param_def_id_to_index[&def_id];
|
let index = generics.param_def_id_to_index[&def_id];
|
||||||
let name = self.tcx.hir().name(hir_id);
|
let name = self.tcx.hir().name(hir_id);
|
||||||
let val = ty::ConstKind::Param(ty::ParamConst::new(index, name));
|
let param = ty::ParamConst::new(index, name);
|
||||||
ExprKind::Literal {
|
|
||||||
literal: self.tcx.mk_const(ty::ConstS {
|
ExprKind::ConstParam { param, def_id }
|
||||||
val,
|
|
||||||
ty: self.typeck_results().node_type(expr.hir_id),
|
|
||||||
}),
|
|
||||||
user_ty: None,
|
|
||||||
const_id: Some(def_id),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
|
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
|
||||||
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
|
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
|
||||||
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
|
ExprKind::NamedConst { def_id, substs, user_ty: user_ty }
|
||||||
ExprKind::Literal {
|
|
||||||
literal: self.tcx.mk_const(ty::ConstS {
|
|
||||||
val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(
|
|
||||||
ty::WithOptConstParam::unknown(def_id),
|
|
||||||
substs,
|
|
||||||
)),
|
|
||||||
ty: self.typeck_results().node_type(expr.hir_id),
|
|
||||||
}),
|
|
||||||
user_ty,
|
|
||||||
const_id: Some(def_id),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
|
Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
use crate::thir::pattern::pat_from_hir;
|
use crate::thir::pattern::pat_from_hir;
|
||||||
use crate::thir::util::UserAnnotatedTyHelpers;
|
use crate::thir::util::UserAnnotatedTyHelpers;
|
||||||
|
|
||||||
use rustc_ast as ast;
|
|
||||||
use rustc_data_structures::steal::Steal;
|
use rustc_data_structures::steal::Steal;
|
||||||
use rustc_errors::ErrorGuaranteed;
|
use rustc_errors::ErrorGuaranteed;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
|
@ -13,9 +12,8 @@ use rustc_hir::def_id::{DefId, LocalDefId};
|
||||||
use rustc_hir::HirId;
|
use rustc_hir::HirId;
|
||||||
use rustc_hir::Node;
|
use rustc_hir::Node;
|
||||||
use rustc_middle::middle::region;
|
use rustc_middle::middle::region;
|
||||||
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
|
|
||||||
use rustc_middle::thir::*;
|
use rustc_middle::thir::*;
|
||||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
use rustc_middle::ty::{self, TyCtxt};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
crate fn thir_body<'tcx>(
|
crate fn thir_body<'tcx>(
|
||||||
|
@ -77,25 +75,6 @@ impl<'tcx> Cx<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn const_eval_literal(
|
|
||||||
&mut self,
|
|
||||||
lit: &'tcx ast::LitKind,
|
|
||||||
ty: Ty<'tcx>,
|
|
||||||
sp: Span,
|
|
||||||
neg: bool,
|
|
||||||
) -> ty::Const<'tcx> {
|
|
||||||
trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
|
|
||||||
|
|
||||||
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) {
|
|
||||||
Ok(c) => c,
|
|
||||||
Err(LitToConstError::Reported) => {
|
|
||||||
// create a dummy value and continue compiling
|
|
||||||
self.tcx.const_error(ty)
|
|
||||||
}
|
|
||||||
Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
|
crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
|
||||||
let p = match self.tcx.hir().get(p.hir_id) {
|
let p = match self.tcx.hir().get(p.hir_id) {
|
||||||
Node::Pat(p) | Node::Binding(p) => p,
|
Node::Pat(p) | Node::Binding(p) => p,
|
||||||
|
|
|
@ -14,7 +14,9 @@ use rustc_hir::def::DefKind;
|
||||||
use rustc_index::vec::IndexVec;
|
use rustc_index::vec::IndexVec;
|
||||||
use rustc_infer::infer::InferCtxt;
|
use rustc_infer::infer::InferCtxt;
|
||||||
use rustc_middle::mir;
|
use rustc_middle::mir;
|
||||||
use rustc_middle::mir::interpret::ErrorHandled;
|
use rustc_middle::mir::interpret::{
|
||||||
|
ConstValue, ErrorHandled, LitToConstError, LitToConstInput, Scalar,
|
||||||
|
};
|
||||||
use rustc_middle::thir;
|
use rustc_middle::thir;
|
||||||
use rustc_middle::thir::abstract_const::{self, Node, NodeId, NotConstEvaluatable};
|
use rustc_middle::thir::abstract_const::{self, Node, NodeId, NotConstEvaluatable};
|
||||||
use rustc_middle::ty::subst::{Subst, SubstsRef};
|
use rustc_middle::ty::subst::{Subst, SubstsRef};
|
||||||
|
@ -28,13 +30,13 @@ use std::iter;
|
||||||
use std::ops::ControlFlow;
|
use std::ops::ControlFlow;
|
||||||
|
|
||||||
/// Check if a given constant can be evaluated.
|
/// Check if a given constant can be evaluated.
|
||||||
|
#[instrument(skip(infcx), level = "debug")]
|
||||||
pub fn is_const_evaluatable<'cx, 'tcx>(
|
pub fn is_const_evaluatable<'cx, 'tcx>(
|
||||||
infcx: &InferCtxt<'cx, 'tcx>,
|
infcx: &InferCtxt<'cx, 'tcx>,
|
||||||
uv: ty::Unevaluated<'tcx, ()>,
|
uv: ty::Unevaluated<'tcx, ()>,
|
||||||
param_env: ty::ParamEnv<'tcx>,
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<(), NotConstEvaluatable> {
|
) -> Result<(), NotConstEvaluatable> {
|
||||||
debug!("is_const_evaluatable({:?})", uv);
|
|
||||||
let tcx = infcx.tcx;
|
let tcx = infcx.tcx;
|
||||||
|
|
||||||
if tcx.features().generic_const_exprs {
|
if tcx.features().generic_const_exprs {
|
||||||
|
@ -304,6 +306,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||||
Err(reported)
|
Err(reported)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(tcx, body, body_id), level = "debug")]
|
||||||
fn new(
|
fn new(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
(body, body_id): (&'a thir::Thir<'tcx>, thir::ExprId),
|
(body, body_id): (&'a thir::Thir<'tcx>, thir::ExprId),
|
||||||
|
@ -315,29 +318,61 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||||
thir: &'a thir::Thir<'tcx>,
|
thir: &'a thir::Thir<'tcx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use crate::rustc_middle::thir::visit::Visitor;
|
||||||
use thir::visit;
|
use thir::visit;
|
||||||
impl<'a, 'tcx: 'a> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
|
|
||||||
|
impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
|
||||||
|
fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
|
||||||
|
if expr.ty.has_param_types_or_consts() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
match expr.kind {
|
||||||
|
thir::ExprKind::NamedConst { substs, .. } => substs.has_param_types_or_consts(),
|
||||||
|
thir::ExprKind::ConstParam { .. } => true,
|
||||||
|
thir::ExprKind::Repeat { value, count } => {
|
||||||
|
self.visit_expr(&self.thir()[value]);
|
||||||
|
count.has_param_types_or_consts()
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
|
||||||
|
if pat.ty.has_param_types_or_consts() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
match pat.kind.as_ref() {
|
||||||
|
thir::PatKind::Constant { value } => value.has_param_types_or_consts(),
|
||||||
|
thir::PatKind::Range(thir::PatRange { lo, hi, .. }) => {
|
||||||
|
lo.has_param_types_or_consts() || hi.has_param_types_or_consts()
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
|
||||||
fn thir(&self) -> &'a thir::Thir<'tcx> {
|
fn thir(&self) -> &'a thir::Thir<'tcx> {
|
||||||
&self.thir
|
&self.thir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), level = "debug")]
|
||||||
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
|
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
|
||||||
self.is_poly |= expr.ty.has_param_types_or_consts();
|
self.is_poly |= self.expr_is_poly(expr);
|
||||||
if !self.is_poly {
|
if !self.is_poly {
|
||||||
visit::walk_expr(self, expr)
|
visit::walk_expr(self, expr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), level = "debug")]
|
||||||
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
|
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
|
||||||
self.is_poly |= pat.ty.has_param_types_or_consts();
|
self.is_poly |= self.pat_is_poly(pat);
|
||||||
if !self.is_poly {
|
if !self.is_poly {
|
||||||
visit::walk_pat(self, pat);
|
visit::walk_pat(self, pat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_const(&mut self, ct: ty::Const<'tcx>) {
|
|
||||||
self.is_poly |= ct.has_param_types_or_consts();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
||||||
|
@ -393,16 +428,49 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||||
fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorGuaranteed> {
|
fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorGuaranteed> {
|
||||||
use thir::ExprKind;
|
use thir::ExprKind;
|
||||||
let node = &self.body.exprs[node];
|
let node = &self.body.exprs[node];
|
||||||
debug!("recurse_build: node={:?}", node);
|
|
||||||
Ok(match &node.kind {
|
Ok(match &node.kind {
|
||||||
// I dont know if handling of these 3 is correct
|
// I dont know if handling of these 3 is correct
|
||||||
&ExprKind::Scope { value, .. } => self.recurse_build(value)?,
|
&ExprKind::Scope { value, .. } => self.recurse_build(value)?,
|
||||||
&ExprKind::PlaceTypeAscription { source, .. }
|
&ExprKind::PlaceTypeAscription { source, .. }
|
||||||
| &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?,
|
| &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?,
|
||||||
|
&ExprKind::Literal { lit, neg} => {
|
||||||
|
let sp = node.span;
|
||||||
|
let constant =
|
||||||
|
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(LitToConstError::Reported) => {
|
||||||
|
self.tcx.const_error(node.ty)
|
||||||
|
}
|
||||||
|
Err(LitToConstError::TypeError) => {
|
||||||
|
bug!("encountered type error in lit_to_const")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// subtle: associated consts are literals this arm handles
|
self.nodes.push(Node::Leaf(constant))
|
||||||
// `<T as Trait>::ASSOC` as well as `12`
|
}
|
||||||
&ExprKind::Literal { literal, .. } => self.nodes.push(Node::Leaf(literal)),
|
&ExprKind::NonHirLiteral { lit , user_ty: _} => {
|
||||||
|
// FIXME Construct a Valtree from this ScalarInt when introducing Valtrees
|
||||||
|
let const_value = ConstValue::Scalar(Scalar::Int(lit));
|
||||||
|
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, const_value, node.ty)))
|
||||||
|
}
|
||||||
|
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
|
||||||
|
let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
|
||||||
|
|
||||||
|
let constant = self.tcx.mk_const(ty::ConstS {
|
||||||
|
val: ty::ConstKind::Unevaluated(uneval),
|
||||||
|
ty: node.ty,
|
||||||
|
});
|
||||||
|
|
||||||
|
self.nodes.push(Node::Leaf(constant))
|
||||||
|
}
|
||||||
|
|
||||||
|
ExprKind::ConstParam {param, ..} => {
|
||||||
|
let const_param = self.tcx.mk_const(ty::ConstS {
|
||||||
|
val: ty::ConstKind::Param(*param),
|
||||||
|
ty: node.ty,
|
||||||
|
});
|
||||||
|
self.nodes.push(Node::Leaf(const_param))
|
||||||
|
}
|
||||||
|
|
||||||
ExprKind::Call { fun, args, .. } => {
|
ExprKind::Call { fun, args, .. } => {
|
||||||
let fun = self.recurse_build(*fun)?;
|
let fun = self.recurse_build(*fun)?;
|
||||||
|
@ -585,6 +653,7 @@ pub(super) fn try_unify_abstract_consts<'tcx>(
|
||||||
// on `ErrorGuaranteed`.
|
// on `ErrorGuaranteed`.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(tcx, f), level = "debug")]
|
||||||
pub fn walk_abstract_const<'tcx, R, F>(
|
pub fn walk_abstract_const<'tcx, R, F>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
ct: AbstractConst<'tcx>,
|
ct: AbstractConst<'tcx>,
|
||||||
|
@ -593,6 +662,7 @@ pub fn walk_abstract_const<'tcx, R, F>(
|
||||||
where
|
where
|
||||||
F: FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
F: FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
||||||
{
|
{
|
||||||
|
#[instrument(skip(tcx, f), level = "debug")]
|
||||||
fn recurse<'tcx, R>(
|
fn recurse<'tcx, R>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
ct: AbstractConst<'tcx>,
|
ct: AbstractConst<'tcx>,
|
||||||
|
@ -600,6 +670,7 @@ where
|
||||||
) -> ControlFlow<R> {
|
) -> ControlFlow<R> {
|
||||||
f(ct)?;
|
f(ct)?;
|
||||||
let root = ct.root(tcx);
|
let root = ct.root(tcx);
|
||||||
|
debug!(?root);
|
||||||
match root {
|
match root {
|
||||||
Node::Leaf(_) => ControlFlow::CONTINUE,
|
Node::Leaf(_) => ControlFlow::CONTINUE,
|
||||||
Node::Binop(_, l, r) => {
|
Node::Binop(_, l, r) => {
|
||||||
|
|
20
src/test/ui/mir/thir-constparam-temp.rs
Normal file
20
src/test/ui/mir/thir-constparam-temp.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
// build-pass
|
||||||
|
|
||||||
|
#![feature(adt_const_params)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
struct Yikes;
|
||||||
|
|
||||||
|
impl Yikes {
|
||||||
|
fn mut_self(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo<const YIKES: Yikes>() {
|
||||||
|
YIKES.mut_self()
|
||||||
|
//~^ WARNING taking a mutable reference
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
foo::<{ Yikes }>()
|
||||||
|
}
|
22
src/test/ui/mir/thir-constparam-temp.stderr
Normal file
22
src/test/ui/mir/thir-constparam-temp.stderr
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
warning: taking a mutable reference to a `const` item
|
||||||
|
--> $DIR/thir-constparam-temp.rs:14:5
|
||||||
|
|
|
||||||
|
LL | YIKES.mut_self()
|
||||||
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `#[warn(const_item_mutation)]` on by default
|
||||||
|
= note: each usage of a `const` item creates a new temporary
|
||||||
|
= note: the mutable reference will refer to this temporary, not the original `const` item
|
||||||
|
note: mutable reference created due to call to this method
|
||||||
|
--> $DIR/thir-constparam-temp.rs:10:5
|
||||||
|
|
|
||||||
|
LL | fn mut_self(&mut self) {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
note: `const` item defined here
|
||||||
|
--> $DIR/thir-constparam-temp.rs:13:14
|
||||||
|
|
|
||||||
|
LL | fn foo<const YIKES: Yikes>() {
|
||||||
|
| ^^^^^
|
||||||
|
|
||||||
|
warning: 1 warning emitted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue