-
Notifications
You must be signed in to change notification settings - Fork 13.4k
make ConstEvaluatable
more strict
#74595
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
c81935e
make `ConstEvaluatable` more strict
lcnr ef6100e
convert to future compat lint
lcnr c10ad0d
review
lcnr 7804644
add tests
lcnr 1dd00e6
add tracking issue, fix rebase
lcnr 4226a17
fix test
lcnr 74e0719
fix test on 32 bit systems
lcnr 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
61 changes: 61 additions & 0 deletions
61
compiler/rustc_trait_selection/src/traits/const_evaluatable.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use rustc_hir::def::DefKind; | ||
use rustc_infer::infer::InferCtxt; | ||
use rustc_middle::mir::interpret::ErrorHandled; | ||
use rustc_middle::ty::subst::SubstsRef; | ||
use rustc_middle::ty::{self, TypeFoldable}; | ||
use rustc_session::lint; | ||
use rustc_span::def_id::DefId; | ||
use rustc_span::Span; | ||
|
||
pub fn is_const_evaluatable<'cx, 'tcx>( | ||
infcx: &InferCtxt<'cx, 'tcx>, | ||
def: ty::WithOptConstParam<DefId>, | ||
substs: SubstsRef<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
span: Span, | ||
) -> Result<(), ErrorHandled> { | ||
let future_compat_lint = || { | ||
if let Some(local_def_id) = def.did.as_local() { | ||
infcx.tcx.struct_span_lint_hir( | ||
lint::builtin::CONST_EVALUATABLE_UNCHECKED, | ||
infcx.tcx.hir().local_def_id_to_hir_id(local_def_id), | ||
span, | ||
|err| { | ||
err.build("cannot use constants which depend on generic parameters in types") | ||
.emit(); | ||
}, | ||
); | ||
} | ||
}; | ||
|
||
// FIXME: We should only try to evaluate a given constant here if it is fully concrete | ||
// as we don't want to allow things like `[u8; std::mem::size_of::<*mut T>()]`. | ||
// | ||
// We previously did not check this, so we only emit a future compat warning if | ||
// const evaluation succeeds and the given constant is still polymorphic for now | ||
// and hopefully soon change this to an error. | ||
// | ||
// See #74595 for more details about this. | ||
let concrete = infcx.const_eval_resolve(param_env, def, substs, None, Some(span)); | ||
|
||
let def_kind = infcx.tcx.def_kind(def.did); | ||
match def_kind { | ||
DefKind::AnonConst => { | ||
let mir_body = if let Some(def) = def.as_const_arg() { | ||
infcx.tcx.optimized_mir_of_const_arg(def) | ||
} else { | ||
infcx.tcx.optimized_mir(def.did) | ||
}; | ||
if mir_body.is_polymorphic && concrete.is_ok() { | ||
future_compat_lint(); | ||
} | ||
} | ||
_ => { | ||
if substs.has_param_types_or_consts() && concrete.is_ok() { | ||
future_compat_lint(); | ||
} | ||
} | ||
} | ||
|
||
concrete.map(drop) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// check-pass | ||
struct Foo<T>(T); | ||
impl<T> Foo<T> { | ||
const VALUE: usize = std::mem::size_of::<T>(); | ||
} | ||
|
||
fn test<T>() { | ||
let _ = [0; Foo::<u8>::VALUE]; | ||
} | ||
|
||
fn main() {} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// check-pass | ||
|
||
const fn foo<T>() -> usize { | ||
// We might instead branch on `std::mem::size_of::<*mut T>() < 8` here, | ||
// which would cause this function to fail on 32 bit systems. | ||
if false { | ||
std::mem::size_of::<T>() | ||
} else { | ||
8 | ||
} | ||
} | ||
|
||
fn test<T>() { | ||
let _ = [0; foo::<T>()]; | ||
//~^ WARN cannot use constants which depend on generic parameters in types | ||
//~| WARN this was previously accepted by the compiler but is being phased out | ||
} | ||
|
||
fn main() {} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning: cannot use constants which depend on generic parameters in types | ||
--> $DIR/function-call.rs:14:17 | ||
| | ||
LL | let _ = [0; foo::<T>()]; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `#[warn(const_evaluatable_unchecked)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200> | ||
|
||
warning: 1 warning emitted | ||
|
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
12 changes: 12 additions & 0 deletions
12
src/test/ui/enum-discriminant/issue-70453-polymorphic-ctfe.stderr
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning: cannot use constants which depend on generic parameters in types | ||
--> $DIR/issue-70453-polymorphic-ctfe.rs:10:15 | ||
| | ||
LL | Some(T) = core::mem::size_of::<*mut T>(), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(const_evaluatable_unchecked)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200> | ||
|
||
warning: 1 warning emitted | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning: cannot use constants which depend on generic parameters in types | ||
--> $DIR/issue-73980.rs:12:9 | ||
| | ||
LL | impl<T> X<T, [u8; L::<T>::S]> {} | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(const_evaluatable_unchecked)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200> | ||
|
||
warning: 1 warning emitted | ||
|
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.