Skip to content

Commit 5238288

Browse files
authored
Rollup merge of rust-lang#145251 - tiif:support_trait, r=BoxyUwU
Support using #[unstable_feature_bound] on trait This is needed to unblock rust-lang#145095 r? ```@BoxyUwU```
2 parents 843d6d9 + bcf87e4 commit 5238288

File tree

7 files changed

+66
-11
lines changed

7 files changed

+66
-11
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ passes_rustc_std_internal_symbol =
669669
.label = not a function or static
670670
671671
passes_rustc_unstable_feature_bound =
672-
attribute should be applied to `impl` or free function outside of any `impl` or trait
673-
.label = not an `impl` or free function
672+
attribute should be applied to `impl`, trait or free function
673+
.label = not an `impl`, trait or free function
674674
675675
passes_should_be_applied_to_fn =
676676
attribute should be applied to a function definition

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23262326
match target {
23272327
// FIXME(staged_api): There's no reason we can't support more targets here. We're just
23282328
// being conservative to begin with.
2329-
Target::Fn | Target::Impl { .. } => {}
2329+
Target::Fn | Target::Impl { .. } | Target::Trait => {}
23302330
Target::ExternCrate
23312331
| Target::Use
23322332
| Target::Static
@@ -2341,7 +2341,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23412341
| Target::Struct
23422342
| Target::Field
23432343
| Target::Union
2344-
| Target::Trait
23452344
| Target::TraitAlias
23462345
| Target::Expression
23472346
| Target::Statement

src/doc/rustc-dev-guide/src/stability.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ of the standard library raises it to a warning with
182182
`#![warn(deprecated_in_future)]`.
183183

184184
## unstable_feature_bound
185-
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]`.
185+
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.
186+
187+
Currently, the items that can be annotated with `#[unstable_feature_bound]` are:
188+
- `impl`
189+
- free function
190+
- trait
186191

187192
[blog]: https://www.ralfj.de/blog/2018/07/19/const.html
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unstable feature `foo` is used without being enabled.
2+
--> $DIR/unstable_feature_bound_on_trait.rs:28:5
3+
|
4+
LL | Foo::bar();
5+
| ^^^^^^^^^^
6+
|
7+
= help: The feature can be enabled by marking the current item with `#[unstable_feature_bound(foo)]`
8+
note: required by a bound in `Bar::bar`
9+
--> $DIR/unstable_feature_bound_on_trait.rs:16:1
10+
|
11+
LL | #[unstable_feature_bound(foo)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Bar::bar`
13+
...
14+
LL | fn bar() {}
15+
| --- required by a bound in this associated function
16+
17+
error: aborting due to 1 previous error
18+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ revisions: pass fail
2+
//@[pass] check-pass
3+
4+
#![allow(internal_features)]
5+
#![feature(staged_api)]
6+
#![stable(feature = "a", since = "1.1.1" )]
7+
8+
/// Test the behaviour of marking a trait with #[unstable_feature_bound].
9+
/// In this testcase, even though the trait method `bar` and the `struct Foo` are
10+
/// both stable, #[unstable_feature_bound] is still needed at the call site of Foo::bar().
11+
12+
#[stable(feature = "a", since = "1.1.1" )]
13+
struct Foo;
14+
15+
#[unstable(feature = "foo", issue = "none" )]
16+
#[unstable_feature_bound(foo)]
17+
trait Bar {
18+
#[stable(feature = "a", since = "1.1.1" )]
19+
fn bar() {}
20+
}
21+
22+
#[unstable_feature_bound(foo)]
23+
impl Bar for Foo {
24+
}
25+
26+
#[cfg_attr(pass, unstable_feature_bound(foo))]
27+
fn moo() {
28+
Foo::bar();
29+
//[fail]~^ ERROR: unstable feature `foo` is used without being enabled.
30+
}
31+
32+
33+
fn main() {}

tests/ui/unstable-feature-bound/unstable_inherent_method.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
pub trait Trait {
1010
#[unstable(feature = "feat", issue = "none" )]
1111
#[unstable_feature_bound(foo)]
12-
//~^ ERROR: attribute should be applied to `impl` or free function outside of any `impl` or trait
12+
//~^ ERROR: attribute should be applied to `impl`, trait or free function
1313
fn foo();
1414
}
1515

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error: attribute should be applied to `impl` or free function outside of any `impl` or trait
1+
error: attribute should be applied to `impl`, trait or free function
22
--> $DIR/unstable_inherent_method.rs:11:5
33
|
44
LL | #[unstable_feature_bound(foo)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
LL |
77
LL | fn foo();
8-
| --------- not an `impl` or free function
8+
| --------- not an `impl`, trait or free function
99

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

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)