Skip to content

allow_internal_unstable attr only allowed once per function #77088

Closed
@tesuji

Description

@tesuji

Discussed in zulip: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/210673352

The problem is that compiler doesn't accept code like this:

    #[allow_internal_unstable(const_fn)]
    #[allow_internal_unstable(const_unreachable_unchecked)]
    pub const fn len(&self) -> usize {
        // SAFETY: this is safe because `&[T]` and `FatPtr<T>` have the same layout.
        // Only `std` can make this guarantee.
        let raw_len = unsafe { crate::ptr::Repr { rust: self }.raw.len };

        match mem::size_of::<T>() {
            0 => {}
            // SAFETY: this branch taken if mathematically `isize::MAX < raw_len * N`.
            // But references must point to one allocation with size at most isize::MAX.
            N if (isize::MAX as usize) / N < raw_len => unsafe { hint::unreachable_unchecked() },
            _ => {}
        }

        raw_len
    }

But it accepts code when merging mutiple allow_internal_unstable annotations into one: #77023

Meta

rustc --version --verbose: Version 1.48.0-nightly (0da5800 2020-09-22)

cc #76807 which modified some const stability checks.

@rustbot modify labels: E-mentor

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions