Skip to content

Support using #[unstable_feature_bound] on trait #145251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ passes_rustc_std_internal_symbol =
.label = not a function or static

passes_rustc_unstable_feature_bound =
attribute should be applied to `impl` or free function outside of any `impl` or trait
.label = not an `impl` or free function
attribute should be applied to `impl`, trait or free function
.label = not an `impl`, trait or free function

passes_should_be_applied_to_fn =
attribute should be applied to a function definition
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
match target {
// FIXME(staged_api): There's no reason we can't support more targets here. We're just
// being conservative to begin with.
Target::Fn | Target::Impl { .. } => {}
Target::Fn | Target::Impl { .. } | Target::Trait => {}
Target::ExternCrate
| Target::Use
| Target::Static
Expand All @@ -2309,7 +2309,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| Target::Struct
| Target::Field
| Target::Union
| Target::Trait
| Target::TraitAlias
| Target::Expression
| Target::Statement
Expand Down
7 changes: 6 additions & 1 deletion src/doc/rustc-dev-guide/src/stability.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ of the standard library raises it to a warning with
`#![warn(deprecated_in_future)]`.

## unstable_feature_bound
The `#[unstable_feature_bound(foo)]` attribute can be used together with `#[unstable]` attribute to mark an `impl` of stable type and stable trait as unstable. In std/core, an item annotated with `#[unstable_feature_bound(foo)]` can only be used by another item that is also annotated with `#[unstable_feature_bound(foo)]`. Outside of std/core, using an item with `#[unstable_feature_bound(foo)]` requires the feature to be enabled with `#![feature(foo)]` attribute on the crate. Currently, only `impl`s and free functions can be annotated with `#[unstable_feature_bound]`.
The `#[unstable_feature_bound(foo)]` attribute can be used together with `#[unstable]` attribute to mark an `impl` of stable type and stable trait as unstable. In std/core, an item annotated with `#[unstable_feature_bound(foo)]` can only be used by another item that is also annotated with `#[unstable_feature_bound(foo)]`. Outside of std/core, using an item with `#[unstable_feature_bound(foo)]` requires the feature to be enabled with `#![feature(foo)]` attribute on the crate.

Currently, the items that can be annotated with `#[unstable_feature_bound]` are:
- `impl`
- free function
- trait

[blog]: https://www.ralfj.de/blog/2018/07/19/const.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: unstable feature `foo` is used without being enabled.
--> $DIR/unstable_feature_bound_on_trait.rs:28:5
|
LL | Foo::bar();
| ^^^^^^^^^^
|
= help: The feature can be enabled by marking the current item with `#[unstable_feature_bound(foo)]`
note: required by a bound in `Bar::bar`
--> $DIR/unstable_feature_bound_on_trait.rs:16:1
|
LL | #[unstable_feature_bound(foo)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Bar::bar`
...
LL | fn bar() {}
| --- required by a bound in this associated function

error: aborting due to 1 previous error

33 changes: 33 additions & 0 deletions tests/ui/unstable-feature-bound/unstable_feature_bound_on_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//@ revisions: pass fail
//@[pass] check-pass

#![allow(internal_features)]
#![feature(staged_api)]
#![stable(feature = "a", since = "1.1.1" )]

/// Test the behaviour of marking a trait with #[unstable_feature_bound].
/// In this testcase, even though the trait method `bar` and the `struct Foo` are
/// both stable, #[unstable_feature_bound] is still needed at the call site of Foo::bar().
#[stable(feature = "a", since = "1.1.1" )]
struct Foo;

#[unstable(feature = "foo", issue = "none" )]
#[unstable_feature_bound(foo)]
trait Bar {
#[stable(feature = "a", since = "1.1.1" )]
fn bar() {}
}

#[unstable_feature_bound(foo)]
impl Bar for Foo {
}

#[cfg_attr(pass, unstable_feature_bound(foo))]
fn moo() {
Foo::bar();
//[fail]~^ ERROR: unstable feature `foo` is used without being enabled.
}


fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/unstable-feature-bound/unstable_inherent_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
pub trait Trait {
#[unstable(feature = "feat", issue = "none" )]
#[unstable_feature_bound(foo)]
//~^ ERROR: attribute should be applied to `impl` or free function outside of any `impl` or trait
//~^ ERROR: attribute should be applied to `impl`, trait or free function
fn foo();
}

#[stable(feature = "a", since = "1.1.1" )]
impl Trait for u8 {
#[unstable_feature_bound(foo)]
//~^ ERROR: attribute should be applied to `impl` or free function outside of any `impl` or trait
//~^ ERROR: attribute should be applied to `impl`, trait or free function
fn foo() {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error: attribute should be applied to `impl` or free function outside of any `impl` or trait
error: attribute should be applied to `impl`, trait or free function
--> $DIR/unstable_inherent_method.rs:11:5
|
LL | #[unstable_feature_bound(foo)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | fn foo();
| --------- not an `impl` or free function
| --------- not an `impl`, trait or free function

error: attribute should be applied to `impl` or free function outside of any `impl` or trait
error: attribute should be applied to `impl`, trait or free function
--> $DIR/unstable_inherent_method.rs:18:5
|
LL | #[unstable_feature_bound(foo)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | fn foo() {}
| ----------- not an `impl` or free function
| ----------- not an `impl`, trait or free function

error: aborting due to 2 previous errors

Loading