Skip to content

Stability annotation on defaulted generics fails #79499

Open

Description

@TimDiekmann

I'm adding <A: AllocRef = Global> to string types. Strings don't have a generic parameter, so the defaulted generic parameter is the first parameter:

pub struct String<#[unstable(feature = "allocator_api", issue = "32838")] A: AllocRef = Global> {}

However, the compiler complains, that this annotation is "useless":

error: This stability annotation is useless
   --> library/alloc/src/string.rs:280:75
    |
280 | pub struct String<#[unstable(feature = "allocator_api", issue = "32838")] A: AllocRef = Global> {
    |                                                                           ^

Was this simply not added by #77118 or is this a false positive in general?

cc @varkor @Avi-D-coder

Activity

  1. added
    A-stabilityArea: `#[stable]`, `#[unstable]` etc.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-allocatorsArea: Custom and system allocators
    on Nov 28, 2020
  2. varkor commented on Nov 28, 2020

    @varkor
    Contributor

    The error is occurring here, for reference:

    // Error if prohibited, or can't inherit anything from a container.
    if kind == AnnotationKind::Prohibited
    || (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
    {
    self.tcx.sess.span_err(item_sp, "This stability annotation is useless");
    }

    Off the top of my head, I'm not sure which part of the condition is satisfied. There's no reason I see to expect this not to work, so it could well have been a case overlooked in #77118.

    @Avi-D-coder: would you be interested in taking a look to see what's going wrong? I should be able to give more immediate response than last time (it'd just take me a little while to find time to debug, myself) :)

  3. zachs18 commented on Sep 6, 2022

    @zachs18
    Contributor

    If I remove derive(PartialOrd, Eq, Ord) from struct String (i.e. manually implement them, like PartialEq already was), then I don't get the This stability annotation is useless error anymore, so I think the error might be coming from a derive macro instead of the struct declaration itself.


    Specifically, for this code (outside of stdlib, because IDK how to cargo expand inside stdlib)

    #![feature(allocator_api)]
    #![feature(std_internals)]
    
    use std::alloc::{Allocator, Global};
    
    #[derive(PartialOrd, Eq, Ord)]
    pub struct String<#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
        vec: Vec<u8, A>,
    }
    impl<A: Allocator, B: Allocator> PartialEq<String<B>> for String<A> {
        fn eq(&self, other: &String<B>) -> bool { todo!() }
    }

    cargo +nightly expand gives

    // error "stability attributes may not be used outside of the standard library" ignored
    // .. etc
    impl<#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator>
        ::core::marker::StructuralEq for String<A>
    {
    }
    // .. etc (None of the other impls have the stability annotation)

    and I think this impl is what's causing the error; it just looks like its the struct definition because the Eq derive macro copies the span from the struct definition.


    If this is correct, maybe this issue should be renamed to something like "Eq derive macro copies stability annotation on generics into impl StructuralEq, causing compilation error"?

    (Either way, if String (/CString/etc) is made allocator-aware, it would be better to manually implement (Partial){Eq,Ord} anyway, so as not to have an <A: Allocator + (Partial){Eq,Ord}> bound.)

  4. SaadiSave commented on Mar 29, 2025

    @SaadiSave

    If work here has stalled, I'd like to make progress on this issue

  5. added 3 commits that reference this issue on Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsA-stabilityArea: `#[stable]`, `#[unstable]` etc.C-bugCategory: This is a bug.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