-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Change Thir to lazily create constants #94876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f713b50
change thir to lazily create constants
b-naber e2496b3
remove thir::Visitor::visit_const
b-naber 9cd8bb0
use ParamConst in ExprKind::ConstParam
b-naber 5e7f138
move ExprKind::Repeat arm to expr_is_poly
b-naber 5fcccd1
use NonHirLiteral instead of ScalarLiteral, move pattern related code…
b-naber 1b5fbe2
add test for treating ExprKind::ConstParam as temp
b-naber 19041d9
dont use a query for lit_to_constant
b-naber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 108 additions & 10 deletions
118
compiler/rustc_mir_build/src/build/expr/as_constant.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,141 @@ | ||
//! See docs in build/expr/mod.rs | ||
|
||
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::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> { | ||
/// Compile `expr`, yielding a compile-time constant. Assumes that | ||
/// `expr` is a valid compile-time constant! | ||
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 tcx = this.tcx; | ||
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; | ||
match *kind { | ||
ExprKind::Scope { region_scope: _, lint_level: _, 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| { | ||
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { | ||
span, | ||
user_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, .. } => { | ||
let const_val = | ||
ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx)); | ||
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 } | ||
} | ||
ExprKind::ConstBlock { value } => { | ||
Constant { span: span, user_ty: None, literal: value.into() } | ||
} | ||
_ => 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)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.