Skip to content

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

Closed

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

  1. added
    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.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    T-langRelevant to the language team
    on Dec 6, 2019
  2. Centril commented on Dec 6, 2019

    @Centril
    Contributor
  3. oli-obk commented on Dec 7, 2019

    @oli-obk
    Contributor

    Oh oh. Investigating...

  4. oli-obk commented on Dec 7, 2019

    @oli-obk
    Contributor

    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 ()

  5. AnthonyMikh commented on Dec 7, 2019

    @AnthonyMikh
    ContributorAuthor

    Interesting: just changing type to i32 is not sufficient, you should actually bind the value to name. This one fails to compile:

    trait ZeroSized: Sized {
        #[deny(const_err)]
        const I_AM_ZERO_SIZED: i32;
        fn requires_zero_size(self);
    }
    
    impl<T: Sized> ZeroSized for T {
        const I_AM_ZERO_SIZED: i32  = [0][std::mem::size_of::<Self>()];
        fn requires_zero_size(self) {
            #![deny(const_err)]
            let _unused = Self::I_AM_ZERO_SIZED;
            println!("requires_zero_size called");
        }
    }
    
    fn main() {
        ().requires_zero_size();
        42_u32.requires_zero_size();
    }

    Making a slight change:

    11 -        let _unused = Self::I_AM_ZERO_SIZED;
    11 +        let _ = Self::I_AM_ZERO_SIZED;
    

    ...And this compiles again on beta and nightly.

  6. self-assigned this
    on Dec 7, 2019
  7. oli-obk commented on Dec 7, 2019

    @oli-obk
    Contributor

    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

  8. RalfJung commented on Dec 9, 2019

    @RalfJung
    Member

    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

    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.

  9. oli-obk commented on Dec 9, 2019

    @oli-obk
    Contributor

    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.

  10. added a commit that references this issue on Dec 10, 2019
  11. pnkfelix commented on Dec 12, 2019

    @pnkfelix
    Contributor

    triage: P-high, removing nomination

  12. removed
    T-langRelevant to the language team
    on Dec 19, 2019
  13. pnkfelix commented on Jan 23, 2020

    @pnkfelix
    Contributor

    fixed by beta backport PR #67297 which was backport of PR #67134

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