-
Notifications
You must be signed in to change notification settings - Fork 1.8k
cast_possible_truncation: Support size_of
, align_of
, and size_of_val
#15488
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
Open
RunDevelopment
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
RunDevelopment:const-eval-size_of
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,7 +23,7 @@ use rustc_middle::ty::{self, FloatTy, IntTy, ScalarInt, Ty, TyCtxt, TypeckResult | |||||||||||
use rustc_middle::{bug, mir, span_bug}; | ||||||||||||
use rustc_span::def_id::DefId; | ||||||||||||
use rustc_span::symbol::Ident; | ||||||||||||
use rustc_span::{SyntaxContext, sym}; | ||||||||||||
use rustc_span::{Symbol, SyntaxContext, sym}; | ||||||||||||
use std::cell::Cell; | ||||||||||||
use std::cmp::Ordering; | ||||||||||||
use std::hash::{Hash, Hasher}; | ||||||||||||
|
@@ -505,22 +505,21 @@ impl<'tcx> ConstEvalCtxt<'tcx> { | |||||||||||
}), | ||||||||||||
ExprKind::If(cond, then, ref otherwise) => self.ifthenelse(cond, then, *otherwise), | ||||||||||||
ExprKind::Binary(op, left, right) => self.binop(op.node, left, right), | ||||||||||||
ExprKind::Call(callee, []) => { | ||||||||||||
// We only handle a few const functions for now. | ||||||||||||
if let ExprKind::Path(qpath) = &callee.kind | ||||||||||||
&& let Some(did) = self.typeck.qpath_res(qpath, callee.hir_id).opt_def_id() | ||||||||||||
{ | ||||||||||||
match self.tcx.get_diagnostic_name(did) { | ||||||||||||
Some(sym::i8_legacy_fn_max_value) => Some(Constant::Int(i8::MAX as u128)), | ||||||||||||
Some(sym::i16_legacy_fn_max_value) => Some(Constant::Int(i16::MAX as u128)), | ||||||||||||
Some(sym::i32_legacy_fn_max_value) => Some(Constant::Int(i32::MAX as u128)), | ||||||||||||
Some(sym::i64_legacy_fn_max_value) => Some(Constant::Int(i64::MAX as u128)), | ||||||||||||
Some(sym::i128_legacy_fn_max_value) => Some(Constant::Int(i128::MAX as u128)), | ||||||||||||
_ => None, | ||||||||||||
} | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
ExprKind::Call(callee, []) => match self.get_fn_diagnostic_name(callee) { | ||||||||||||
Some(sym::i8_legacy_fn_max_value) => Some(Constant::Int(i8::MAX as u128)), | ||||||||||||
Some(sym::i16_legacy_fn_max_value) => Some(Constant::Int(i16::MAX as u128)), | ||||||||||||
Some(sym::i32_legacy_fn_max_value) => Some(Constant::Int(i32::MAX as u128)), | ||||||||||||
Some(sym::i64_legacy_fn_max_value) => Some(Constant::Int(i64::MAX as u128)), | ||||||||||||
Some(sym::i128_legacy_fn_max_value) => Some(Constant::Int(i128::MAX as u128)), | ||||||||||||
Some(sym::mem_align_of) => self.align_of_call(callee), | ||||||||||||
Some(sym::mem_size_of) => self.size_of_call(callee), | ||||||||||||
_ => None, | ||||||||||||
}, | ||||||||||||
ExprKind::Call(callee, [_]) => match self.get_fn_diagnostic_name(callee) { | ||||||||||||
// `align_of_val` doesn't have a diagnostic name, unfortunately. | ||||||||||||
// Some(sym::mem_align_of_val) => self.align_of_call(callee), | ||||||||||||
Some(sym::mem_size_of_val) => self.size_of_call(callee), | ||||||||||||
_ => None, | ||||||||||||
Comment on lines
+518
to
+522
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the other comment
Suggested change
|
||||||||||||
}, | ||||||||||||
ExprKind::Index(arr, index, _) => self.index(arr, index), | ||||||||||||
ExprKind::AddrOf(_, _, inner) => self.expr(inner).map(|r| Constant::Ref(Box::new(r))), | ||||||||||||
|
@@ -854,6 +853,35 @@ impl<'tcx> ConstEvalCtxt<'tcx> { | |||||||||||
}, | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn get_fn_diagnostic_name(&self, callee: &Expr<'_>) -> Option<Symbol> { | ||||||||||||
if let ExprKind::Path(qpath) = &callee.kind | ||||||||||||
&& let Some(def_id) = self.typeck.qpath_res(qpath, callee.hir_id).opt_def_id() | ||||||||||||
{ | ||||||||||||
self.tcx.get_diagnostic_name(def_id) | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn align_of_call(&self, callee: &Expr<'_>) -> Option<Constant<'tcx>> { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add some documentation here for these two functions? |
||||||||||||
let ty = self.typeck.node_args(callee.hir_id).types().next()?; | ||||||||||||
if let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(ty)) { | ||||||||||||
Some(Constant::Int(u128::from(layout.align.bytes()))) | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
} | ||||||||||||
fn size_of_call(&self, callee: &Expr<'_>) -> Option<Constant<'tcx>> { | ||||||||||||
let ty = self.typeck.node_args(callee.hir_id).types().next()?; | ||||||||||||
if let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(ty)) | ||||||||||||
&& layout.is_sized() | ||||||||||||
{ | ||||||||||||
Some(Constant::Int(u128::from(layout.size.bytes()))) | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::Const<'tcx>) -> Option<Constant<'tcx>> { | ||||||||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[..]
The rest pattern also allows for zero-sized arrays, no need for the extra branch.