Repository navigation
Closed
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.P-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.Performance or correctness regression from stable to beta.
Description
Consider the following code:
trait ZeroSized: Sized {
#[deny(const_err)]
const I_AM_ZERO_SIZED: ();
fn requires_zero_size(self);
}
impl<T: Sized> ZeroSized for T {
const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()];
fn requires_zero_size(self) {
#![deny(const_err)]
let () = Self::I_AM_ZERO_SIZED;
println!("requires_zero_size called");
}
}
fn main() {
().requires_zero_size();
42_u32.requires_zero_size();
}The intent of code is to prevent requires_zero_size from being called on types with non-zero size (duh). This goal is achieved on stable: trying to compile this code with stable compiler (1.39.0) gives the following error:
error: any use of this value will cause an error
--> src/main.rs:8:34
|
8 | const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()];
| -----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| index out of bounds: the len is 1 but the index is 4
|
= note: `#[deny(const_err)]` on by default
I expected the same with some newer unstable versiosns. However, this code compiles just fine on latest beta (2019-12-02 1463fedbddfc0ee087ec) and latest nightly (2019-12-05 710a362dc7634fce4288) at the moment.
Compiling in debug vs release gives no difference in behaviour.
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.P-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.Performance or correctness regression from stable to beta.
Activity
Centril commentedon Dec 6, 2019
cc @wesleywiser @oli-obk
oli-obk commentedon Dec 7, 2019
Oh oh. Investigating...
oli-obk commentedon Dec 7, 2019
Wild guess: const prop based on miri does not actually read from zst constants... and confirmed by making the constant's type i32 instead of ()
AnthonyMikh commentedon Dec 7, 2019
Interesting: just changing type to
i32is not sufficient, you should actually bind the value to name. This one fails to compile:Making a slight change:
...And this compiles again on beta and nightly.
oli-obk commentedon Dec 7, 2019
It's not const prop. Const prop doesn't know about this yet. If you allow all the lints on stable, you get a segfault in the running program. I think monomorphization needs to hard error on invalid constants. Trying out now
RalfJung commentedon Dec 9, 2019
Notice that seems quite fragile though, as this is only prevented by a deny-by-default lint. With
--cap-lints(as it is used for dependencies) or when someone uses your crate with#[allow(const_err)], this code will compile on stable already.oli-obk commentedon Dec 9, 2019
Usage of a broken constant is not just a lint, it is a hard error in monomorphic code. What you are thinking about is promotion, where we never report a hard error. Using a constant whose evaluation would fail is a hard error, it's just that declaring said constant and not using it will just cause warnings.
Rollup merge of rust-lang#67134 - oli-obk:const_prop_zst, r=wesleywiser
pnkfelix commentedon Dec 12, 2019
triage: P-high, removing nomination
pnkfelix commentedon Jan 23, 2020
fixed by beta backport PR #67297 which was backport of PR #67134