Skip to content

Usage of errorneous constant can be omitted on nightly and beta #67083

Description

@AnthonyMikh

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-associated-itemsArea: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant 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.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions