Skip to content

Commit 857afc7

Browse files
authoredJul 20, 2022
Rollup merge of #99212 - davidtwco:partial-stability-implies, r=michaelwoerister
introduce `implied_by` in `#[unstable]` attribute Requested by the library team [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/better.20support.20for.20partial.20stabilizations/near/285581519). If part of a feature is stabilized and a new feature is added for the remaining parts, then the `implied_by` meta-item can be added to `#[unstable]` to indicate which now-stable feature was used previously. ```diagnostic error: the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar` --> $DIR/stability-attribute-implies-using-unstable.rs:3:12 | LL | #![feature(foo)] | ^^^ | note: the lint level is defined here --> $DIR/stability-attribute-implies-using-stable.rs:2:9 | LL | #![deny(stable_features)] | ^^^^^^^^^^^^^^^ help: if you are using features which are still unstable, change to using `foobar` | LL | #![feature(foobar)] | ~~~~~~ help: if you are using features which are now stable, remove this line | LL - #![feature(foo)] | ``` When a `#![feature(..)]` attribute still exists for the now-stable attribute, then there this has two effects: - There will not be an stability error for uses of items from the implied feature which are still unstable (until the `#![feature(..)]` is removed or updated to the new feature). - There will be an improved diagnostic for the remaining use of the feature attribute for the now-stable feature. ```rust /// If part of a feature is stabilized and a new feature is added for the remaining parts, /// then the `implied_by` attribute is used to indicate which now-stable feature previously /// contained a item. /// /// ```pseudo-Rust /// #[unstable(feature = "foo", issue = "...")] /// fn foo() {} /// #[unstable(feature = "foo", issue = "...")] /// fn foobar() {} /// ``` /// /// ...becomes... /// /// ```pseudo-Rust /// #[stable(feature = "foo", since = "1.XX.X")] /// fn foo() {} /// #[unstable(feature = "foobar", issue = "...", implied_by = "foo")] /// fn foobar() {} /// ``` ``` In the Zulip discussion, this was envisioned as `implies` on `#[stable]` but I went with `implied_by` on `#[unstable]` because it means that only the unstable attribute needs to be changed in future, not the new stable attribute, which seems less error-prone. It also isn't particularly feasible for me to detect whether items from the implied feature are used and then only suggest updating _or_ removing the `#![feature(..)]` as appropriate, so I always do both. There's some new information in the cross-crate metadata as a result of this change, that's a little unfortunate, but without requiring that the `#[unstable]` and `#[stable]` attributes both contain the implication information, it's necessary: ```rust /// This mapping is necessary unless both the `#[stable]` and `#[unstable]` attributes should /// specify their implications (both `implies` and `implied_by`). If only one of the two /// attributes do (as in the current implementation, `implied_by` in `#[unstable]`), then this /// mapping is necessary for diagnostics. When a "unnecessary feature attribute" error is /// reported, only the `#[stable]` attribute information is available, so the map is necessary /// to know that the feature implies another feature. If it were reversed, and the `#[stable]` /// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of /// unstable feature" error for a feature that was implied. ``` I also change some comments to documentation comments in the compiler, add a helper for going from a `Span` to a `Span` for the entire line, and fix a incorrect part of the pre-existing stability attribute diagnostics. cc `@yaahc`
·
1.90.01.64.0
2 parents 14dbfeb + e587299 commit 857afc7

24 files changed

+357
-62
lines changed
 

‎compiler/rustc_attr/src/builtin.rs‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,42 @@ impl ConstStability {
135135
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
136136
#[derive(HashStable_Generic)]
137137
pub enum StabilityLevel {
138-
// Reason for the current stability level and the relevant rust-lang issue
139-
Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
140-
Stable { since: Symbol, allowed_through_unstable_modules: bool },
138+
/// `#[unstable]`
139+
Unstable {
140+
/// Reason for the current stability level.
141+
reason: Option<Symbol>,
142+
/// Relevant `rust-lang/rust` issue.
143+
issue: Option<NonZeroU32>,
144+
is_soft: bool,
145+
/// If part of a feature is stabilized and a new feature is added for the remaining parts,
146+
/// then the `implied_by` attribute is used to indicate which now-stable feature previously
147+
/// contained a item.
148+
///
149+
/// ```pseudo-Rust
150+
/// #[unstable(feature = "foo", issue = "...")]
151+
/// fn foo() {}
152+
/// #[unstable(feature = "foo", issue = "...")]
153+
/// fn foobar() {}
154+
/// ```
155+
///
156+
/// ...becomes...
157+
///
158+
/// ```pseudo-Rust
159+
/// #[stable(feature = "foo", since = "1.XX.X")]
160+
/// fn foo() {}
161+
/// #[unstable(feature = "foobar", issue = "...", implied_by = "foo")]
162+
/// fn foobar() {}
163+
/// ```
164+
implied_by: Option<Symbol>,
165+
},
166+
/// `#[stable]`
167+
Stable {
168+
/// Rust release which stabilized this feature.
169+
since: Symbol,
170+
/// Is this item allowed to be referred to on stable, despite being contained in unstable
171+
/// modules?
172+
allowed_through_unstable_modules: bool,
173+
},
141174
}
142175

143176
impl StabilityLevel {
@@ -243,6 +276,7 @@ where
243276
let mut issue = None;
244277
let mut issue_num = None;
245278
let mut is_soft = false;
279+
let mut implied_by = None;
246280
for meta in metas {
247281
let Some(mi) = meta.meta_item() else {
248282
handle_errors(
@@ -308,6 +342,11 @@ where
308342
}
309343
is_soft = true;
310344
}
345+
sym::implied_by => {
346+
if !get(mi, &mut implied_by) {
347+
continue 'outer;
348+
}
349+
}
311350
_ => {
312351
handle_errors(
313352
&sess.parse_sess,
@@ -332,7 +371,7 @@ where
332371
);
333372
continue;
334373
}
335-
let level = Unstable { reason, issue: issue_num, is_soft };
374+
let level = Unstable { reason, issue: issue_num, is_soft, implied_by };
336375
if sym::unstable == meta_name {
337376
stab = Some((Stability { level, feature }, attr.span));
338377
} else {
@@ -391,7 +430,7 @@ where
391430
meta.span(),
392431
AttrError::UnknownMetaItem(
393432
pprust::path_to_string(&mi.path),
394-
&["since", "note"],
433+
&["feature", "since"],
395434
),
396435
);
397436
continue 'outer;

‎compiler/rustc_metadata/src/rmeta/decoder.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
951951
tcx.arena.alloc_from_iter(self.root.lib_features.decode(self))
952952
}
953953

954+
/// Iterates over the stability implications in the given crate (when a `#[unstable]` attribute
955+
/// has an `implied_by` meta item, then the mapping from the implied feature to the actual
956+
/// feature is a stability implication).
957+
fn get_stability_implications(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Symbol)] {
958+
tcx.arena.alloc_from_iter(self.root.stability_implications.decode(self))
959+
}
960+
954961
/// Iterates over the language items in the given crate.
955962
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
956963
tcx.arena.alloc_from_iter(

‎compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
291291
tcx.arena.alloc_slice(&result)
292292
}
293293
defined_lib_features => { cdata.get_lib_features(tcx) }
294+
stability_implications => {
295+
cdata.get_stability_implications(tcx).iter().copied().collect()
296+
}
294297
is_intrinsic => { cdata.get_is_intrinsic(def_id.index) }
295298
defined_lang_items => { cdata.get_lang_items(tcx) }
296299
diagnostic_items => { cdata.get_diagnostic_items() }

‎compiler/rustc_metadata/src/rmeta/encoder.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
538538
let lib_features = self.encode_lib_features();
539539
let lib_feature_bytes = self.position() - i;
540540

541+
// Encode the stability implications.
542+
i = self.position();
543+
let stability_implications = self.encode_stability_implications();
544+
let stability_implications_bytes = self.position() - i;
545+
541546
// Encode the language items.
542547
i = self.position();
543548
let lang_items = self.encode_lang_items();
@@ -686,6 +691,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
686691
crate_deps,
687692
dylib_dependency_formats,
688693
lib_features,
694+
stability_implications,
689695
lang_items,
690696
diagnostic_items,
691697
lang_items_missing,
@@ -710,6 +716,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
710716
let computed_total_bytes = preamble_bytes
711717
+ dep_bytes
712718
+ lib_feature_bytes
719+
+ stability_implications_bytes
713720
+ lang_item_bytes
714721
+ diagnostic_item_bytes
715722
+ native_lib_bytes
@@ -761,6 +768,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
761768
p("preamble", preamble_bytes);
762769
p("dep", dep_bytes);
763770
p("lib feature", lib_feature_bytes);
771+
p("stability_implications", stability_implications_bytes);
764772
p("lang item", lang_item_bytes);
765773
p("diagnostic item", diagnostic_item_bytes);
766774
p("native lib", native_lib_bytes);
@@ -1777,6 +1785,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17771785
self.lazy_array(lib_features.to_vec())
17781786
}
17791787

1788+
fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> {
1789+
empty_proc_macro!(self);
1790+
let tcx = self.tcx;
1791+
let implications = tcx.stability_implications(LOCAL_CRATE);
1792+
self.lazy_array(implications.iter().map(|(k, v)| (*k, *v)))
1793+
}
1794+
17801795
fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> {
17811796
empty_proc_macro!(self);
17821797
let tcx = self.tcx;

‎compiler/rustc_metadata/src/rmeta/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ pub(crate) struct CrateRoot {
226226
crate_deps: LazyArray<CrateDep>,
227227
dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
228228
lib_features: LazyArray<(Symbol, Option<Symbol>)>,
229+
stability_implications: LazyArray<(Symbol, Symbol)>,
229230
lang_items: LazyArray<(DefIndex, usize)>,
230231
lang_items_missing: LazyArray<lang_items::LangItem>,
231232
diagnostic_items: LazyArray<(Symbol, DefIndex)>,

‎compiler/rustc_middle/src/middle/mod.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ pub mod dependency_format;
33
pub mod exported_symbols;
44
pub mod lang_items;
55
pub mod lib_features {
6-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7-
use rustc_span::symbol::Symbol;
6+
use rustc_data_structures::fx::FxHashMap;
7+
use rustc_span::{symbol::Symbol, Span};
88

99
#[derive(HashStable, Debug)]
1010
pub struct LibFeatures {
11-
// A map from feature to stabilisation version.
12-
pub stable: FxHashMap<Symbol, Symbol>,
13-
pub unstable: FxHashSet<Symbol>,
11+
/// A map from feature to stabilisation version.
12+
pub stable: FxHashMap<Symbol, (Symbol, Span)>,
13+
pub unstable: FxHashMap<Symbol, Span>,
1414
}
1515

1616
impl LibFeatures {
1717
pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
1818
let mut all_features: Vec<_> = self
1919
.stable
2020
.iter()
21-
.map(|(f, s)| (*f, Some(*s)))
22-
.chain(self.unstable.iter().map(|f| (*f, None)))
21+
.map(|(f, (s, _))| (*f, Some(*s)))
22+
.chain(self.unstable.iter().map(|(f, _)| (*f, None)))
2323
.collect();
2424
all_features.sort_unstable_by(|a, b| a.0.as_str().partial_cmp(b.0.as_str()).unwrap());
2525
all_features

‎compiler/rustc_middle/src/middle/stability.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ pub struct Index {
6262
pub stab_map: FxHashMap<LocalDefId, Stability>,
6363
pub const_stab_map: FxHashMap<LocalDefId, ConstStability>,
6464
pub depr_map: FxHashMap<LocalDefId, DeprecationEntry>,
65+
/// Mapping from feature name to feature name based on the `implied_by` field of `#[unstable]`
66+
/// attributes. If a `#[unstable(feature = "implier", implied_by = "impliee")]` attribute
67+
/// exists, then this map will have a `impliee -> implier` entry.
68+
///
69+
/// This mapping is necessary unless both the `#[stable]` and `#[unstable]` attributes should
70+
/// specify their implications (both `implies` and `implied_by`). If only one of the two
71+
/// attributes do (as in the current implementation, `implied_by` in `#[unstable]`), then this
72+
/// mapping is necessary for diagnostics. When a "unnecessary feature attribute" error is
73+
/// reported, only the `#[stable]` attribute information is available, so the map is necessary
74+
/// to know that the feature implies another feature. If it were reversed, and the `#[stable]`
75+
/// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of
76+
/// unstable feature" error for a feature that was implied.
77+
pub implications: FxHashMap<Symbol, Symbol>,
6578
}
6679

6780
impl Index {
@@ -423,7 +436,9 @@ impl<'tcx> TyCtxt<'tcx> {
423436

424437
match stability {
425438
Some(Stability {
426-
level: attr::Unstable { reason, issue, is_soft }, feature, ..
439+
level: attr::Unstable { reason, issue, is_soft, implied_by },
440+
feature,
441+
..
427442
}) => {
428443
if span.allows_unstable(feature) {
429444
debug!("stability: skipping span={:?} since it is internal", span);
@@ -433,6 +448,13 @@ impl<'tcx> TyCtxt<'tcx> {
433448
return EvalResult::Allow;
434449
}
435450

451+
// If this item was previously part of a now-stabilized feature which is still
452+
// active (i.e. the user hasn't removed the attribute for the stabilized feature
453+
// yet) then allow use of this item.
454+
if let Some(implied_by) = implied_by && self.features().active(implied_by) {
455+
return EvalResult::Allow;
456+
}
457+
436458
// When we're compiling the compiler itself we may pull in
437459
// crates from crates.io, but those crates may depend on other
438460
// crates also pulled in from crates.io. We want to ideally be

‎compiler/rustc_middle/src/query/mod.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,11 +1634,15 @@ rustc_queries! {
16341634
storage(ArenaCacheSelector<'tcx>)
16351635
desc { "calculating the lib features map" }
16361636
}
1637-
query defined_lib_features(_: CrateNum)
1638-
-> &'tcx [(Symbol, Option<Symbol>)] {
1637+
query defined_lib_features(_: CrateNum) -> &'tcx [(Symbol, Option<Symbol>)] {
16391638
desc { "calculating the lib features defined in a crate" }
16401639
separate_provide_extern
16411640
}
1641+
query stability_implications(_: CrateNum) -> FxHashMap<Symbol, Symbol> {
1642+
storage(ArenaCacheSelector<'tcx>)
1643+
desc { "calculating the implications between `#[unstable]` features defined in a crate" }
1644+
separate_provide_extern
1645+
}
16421646
/// Whether the function is an intrinsic
16431647
query is_intrinsic(def_id: DefId) -> bool {
16441648
desc { |tcx| "is_intrinsic({})", tcx.def_path_str(def_id) }

‎compiler/rustc_passes/src/lib_features.rs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Detecting lib features (i.e., features that are not lang features).
2-
//
3-
// These are declared using stability attributes (e.g., `#[stable (..)]`
4-
// and `#[unstable (..)]`), but are not declared in one single location
5-
// (unlike lang features), which means we need to collect them instead.
1+
//! Detecting lib features (i.e., features that are not lang features).
2+
//!
3+
//! These are declared using stability attributes (e.g., `#[stable (..)]` and `#[unstable (..)]`),
4+
//! but are not declared in one single location (unlike lang features), which means we need to
5+
//! collect them instead.
66
77
use rustc_ast::{Attribute, MetaItemKind};
88
use rustc_errors::struct_span_err;
@@ -71,11 +71,11 @@ impl<'tcx> LibFeatureCollector<'tcx> {
7171

7272
fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
7373
let already_in_stable = self.lib_features.stable.contains_key(&feature);
74-
let already_in_unstable = self.lib_features.unstable.contains(&feature);
74+
let already_in_unstable = self.lib_features.unstable.contains_key(&feature);
7575

7676
match (since, already_in_stable, already_in_unstable) {
7777
(Some(since), _, false) => {
78-
if let Some(prev_since) = self.lib_features.stable.get(&feature) {
78+
if let Some((prev_since, _)) = self.lib_features.stable.get(&feature) {
7979
if *prev_since != since {
8080
self.span_feature_error(
8181
span,
@@ -89,10 +89,10 @@ impl<'tcx> LibFeatureCollector<'tcx> {
8989
}
9090
}
9191

92-
self.lib_features.stable.insert(feature, since);
92+
self.lib_features.stable.insert(feature, (since, span));
9393
}
9494
(None, false, _) => {
95-
self.lib_features.unstable.insert(feature);
95+
self.lib_features.unstable.insert(feature, span);
9696
}
9797
(Some(_), _, true) | (None, true, _) => {
9898
self.span_feature_error(

‎compiler/rustc_passes/src/stability.rs‎

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//! propagating default levels lexically from parent to children ast nodes.
33
44
use attr::StabilityLevel;
5-
use rustc_attr::{self as attr, ConstStability, Stability};
6-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
7-
use rustc_errors::struct_span_err;
5+
use rustc_attr::{self as attr, ConstStability, Stability, Unstable};
6+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
7+
use rustc_errors::{struct_span_err, Applicability};
88
use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -29,13 +29,13 @@ use std::num::NonZeroU32;
2929

3030
#[derive(PartialEq)]
3131
enum AnnotationKind {
32-
// Annotation is required if not inherited from unstable parents
32+
/// Annotation is required if not inherited from unstable parents.
3333
Required,
34-
// Annotation is useless, reject it
34+
/// Annotation is useless, reject it.
3535
Prohibited,
36-
// Deprecation annotation is useless, reject it. (Stability attribute is still required.)
36+
/// Deprecation annotation is useless, reject it. (Stability attribute is still required.)
3737
DeprecationProhibited,
38-
// Annotation itself is useless, but it can be propagated to children
38+
/// Annotation itself is useless, but it can be propagated to children.
3939
Container,
4040
}
4141

@@ -83,7 +83,7 @@ impl InheritStability {
8383
}
8484
}
8585

86-
// A private tree-walker for producing an Index.
86+
/// A private tree-walker for producing an `Index`.
8787
struct Annotator<'a, 'tcx> {
8888
tcx: TyCtxt<'tcx>,
8989
index: &'a mut Index,
@@ -94,9 +94,9 @@ struct Annotator<'a, 'tcx> {
9494
}
9595

9696
impl<'a, 'tcx> Annotator<'a, 'tcx> {
97-
// Determine the stability for a node based on its attributes and inherited
98-
// stability. The stability is recorded in the index and used as the parent.
99-
// If the node is a function, `fn_sig` is its signature
97+
/// Determine the stability for a node based on its attributes and inherited stability. The
98+
/// stability is recorded in the index and used as the parent. If the node is a function,
99+
/// `fn_sig` is its signature.
100100
fn annotate<F>(
101101
&mut self,
102102
def_id: LocalDefId,
@@ -265,6 +265,10 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
265265
}
266266
}
267267

268+
if let Stability { level: Unstable { implied_by: Some(implied_by), .. }, feature } = stab {
269+
self.index.implications.insert(implied_by, feature);
270+
}
271+
268272
self.index.stab_map.insert(def_id, stab);
269273
stab
270274
});
@@ -610,6 +614,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
610614
stab_map: Default::default(),
611615
const_stab_map: Default::default(),
612616
depr_map: Default::default(),
617+
implications: Default::default(),
613618
};
614619

615620
{
@@ -637,6 +642,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
637642
reason: Some(Symbol::intern(reason)),
638643
issue: NonZeroU32::new(27812),
639644
is_soft: false,
645+
implied_by: None,
640646
},
641647
feature: sym::rustc_private,
642648
};
@@ -667,6 +673,7 @@ pub(crate) fn provide(providers: &mut Providers) {
667673
*providers = Providers {
668674
check_mod_unstable_api_usage,
669675
stability_index,
676+
stability_implications: |tcx, _| tcx.stability().implications.clone(),
670677
lookup_stability: |tcx, id| tcx.stability().local_stability(id.expect_local()),
671678
lookup_const_stability: |tcx, id| tcx.stability().local_const_stability(id.expect_local()),
672679
lookup_deprecation_entry: |tcx, id| {
@@ -945,32 +952,51 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
945952
remaining_lib_features.remove(&sym::libc);
946953
remaining_lib_features.remove(&sym::test);
947954

948-
let check_features = |remaining_lib_features: &mut FxIndexMap<_, _>, defined_features: &[_]| {
949-
for &(feature, since) in defined_features {
950-
if let Some(since) = since {
951-
if let Some(span) = remaining_lib_features.get(&feature) {
952-
// Warn if the user has enabled an already-stable lib feature.
953-
unnecessary_stable_feature_lint(tcx, *span, feature, since);
954-
}
955-
}
956-
remaining_lib_features.remove(&feature);
957-
if remaining_lib_features.is_empty() {
958-
break;
959-
}
960-
}
961-
};
962-
963955
// We always collect the lib features declared in the current crate, even if there are
964956
// no unknown features, because the collection also does feature attribute validation.
965-
let local_defined_features = tcx.lib_features(()).to_vec();
966-
if !remaining_lib_features.is_empty() {
967-
check_features(&mut remaining_lib_features, &local_defined_features);
957+
let local_defined_features = tcx.lib_features(());
958+
let mut all_lib_features: FxHashMap<_, _> =
959+
local_defined_features.to_vec().iter().map(|el| *el).collect();
960+
let mut implications = tcx.stability_implications(rustc_hir::def_id::LOCAL_CRATE).clone();
961+
for &cnum in tcx.crates(()) {
962+
implications.extend(tcx.stability_implications(cnum));
963+
all_lib_features.extend(tcx.defined_lib_features(cnum).iter().map(|el| *el));
964+
}
968965

969-
for &cnum in tcx.crates(()) {
966+
// Check that every feature referenced by an `implied_by` exists (for features defined in the
967+
// local crate).
968+
for (implied_by, feature) in tcx.stability_implications(rustc_hir::def_id::LOCAL_CRATE) {
969+
// Only `implied_by` needs to be checked, `feature` is guaranteed to exist.
970+
if !all_lib_features.contains_key(implied_by) {
971+
let span = local_defined_features
972+
.stable
973+
.get(feature)
974+
.map(|(_, span)| span)
975+
.or_else(|| local_defined_features.unstable.get(feature))
976+
.expect("feature that implied another does not exist");
977+
tcx.sess
978+
.struct_span_err(
979+
*span,
980+
format!("feature `{implied_by}` implying `{feature}` does not exist"),
981+
)
982+
.emit();
983+
}
984+
}
985+
986+
if !remaining_lib_features.is_empty() {
987+
for (feature, since) in all_lib_features.iter() {
988+
if let Some(since) = since && let Some(span) = remaining_lib_features.get(&feature) {
989+
// Warn if the user has enabled an already-stable lib feature.
990+
if let Some(implies) = implications.get(&feature) {
991+
unnecessary_partially_stable_feature_lint(tcx, *span, *feature, *implies, *since);
992+
} else {
993+
unnecessary_stable_feature_lint(tcx, *span, *feature, *since);
994+
}
995+
}
996+
remaining_lib_features.remove(&feature);
970997
if remaining_lib_features.is_empty() {
971998
break;
972999
}
973-
check_features(&mut remaining_lib_features, tcx.defined_lib_features(cnum));
9741000
}
9751001
}
9761002

@@ -982,12 +1008,41 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
9821008
// don't lint about unused features. We should re-enable this one day!
9831009
}
9841010

1011+
fn unnecessary_partially_stable_feature_lint(
1012+
tcx: TyCtxt<'_>,
1013+
span: Span,
1014+
feature: Symbol,
1015+
implies: Symbol,
1016+
since: Symbol,
1017+
) {
1018+
tcx.struct_span_lint_hir(lint::builtin::STABLE_FEATURES, hir::CRATE_HIR_ID, span, |lint| {
1019+
lint.build(&format!(
1020+
"the feature `{feature}` has been partially stabilized since {since} and is succeeded \
1021+
by the feature `{implies}`"
1022+
))
1023+
.span_suggestion(
1024+
span,
1025+
&format!(
1026+
"if you are using features which are still unstable, change to using `{implies}`"
1027+
),
1028+
implies,
1029+
Applicability::MaybeIncorrect,
1030+
)
1031+
.span_suggestion(
1032+
tcx.sess.source_map().span_extend_to_line(span),
1033+
"if you are using features which are now stable, remove this line",
1034+
"",
1035+
Applicability::MaybeIncorrect,
1036+
)
1037+
.emit();
1038+
});
1039+
}
1040+
9851041
fn unnecessary_stable_feature_lint(tcx: TyCtxt<'_>, span: Span, feature: Symbol, since: Symbol) {
9861042
tcx.struct_span_lint_hir(lint::builtin::STABLE_FEATURES, hir::CRATE_HIR_ID, span, |lint| {
9871043
lint.build(&format!(
988-
"the feature `{}` has been stable since {} and no longer requires \
989-
an attribute to enable",
990-
feature, since
1044+
"the feature `{feature}` has been stable since {since} and no longer requires an \
1045+
attribute to enable",
9911046
))
9921047
.emit();
9931048
});

‎compiler/rustc_resolve/src/macros.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,16 @@ impl<'a> Resolver<'a> {
796796
) {
797797
let span = path.span;
798798
if let Some(stability) = &ext.stability {
799-
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
799+
if let StabilityLevel::Unstable { reason, issue, is_soft, implied_by } = stability.level
800+
{
800801
let feature = stability.feature;
801-
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
802+
803+
let is_allowed = |feature| {
804+
self.active_features.contains(&feature) || span.allows_unstable(feature)
805+
};
806+
let allowed_by_implication =
807+
implied_by.map(|feature| is_allowed(feature)).unwrap_or(false);
808+
if !is_allowed(feature) && !allowed_by_implication {
802809
let lint_buffer = &mut self.lint_buffer;
803810
let soft_handler =
804811
|lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);

‎compiler/rustc_span/src/source_map.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ impl SourceMap {
718718
sp
719719
}
720720

721+
/// Extends the given `Span` to contain the entire line it is on.
722+
pub fn span_extend_to_line(&self, sp: Span) -> Span {
723+
self.span_extend_to_prev_char(self.span_extend_to_next_char(sp, '\n', true), '\n', true)
724+
}
725+
721726
/// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
722727
/// `c`.
723728
pub fn span_until_char(&self, sp: Span, c: char) -> Span {

‎compiler/rustc_span/src/symbol.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ symbols! {
800800
impl_lint_pass,
801801
impl_macros,
802802
impl_trait_in_bindings,
803+
implied_by,
803804
import,
804805
import_shadowing,
805806
imported_main,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(staged_api)]
2+
#![stable(feature = "stability_attribute_implies", since = "1.0.0")]
3+
4+
#[stable(feature = "foo", since = "1.62.0")]
5+
pub fn foo() {}
6+
7+
#[unstable(feature = "foobar", issue = "1", implied_by = "foo")]
8+
pub fn foobar() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(staged_api)]
2+
#![stable(feature = "stability_attribute_implies", since = "1.0.0")]
3+
4+
// Tests that `implied_by = "bar"` results in an error being emitted if `bar` does not exist.
5+
6+
#[unstable(feature = "foobar", issue = "1", implied_by = "bar")]
7+
//~^ ERROR feature `bar` implying `foobar` does not exist
8+
pub fn foobar() {}
9+
10+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: feature `bar` implying `foobar` does not exist
2+
--> $DIR/stability-attribute-implies-missing.rs:6:1
3+
|
4+
LL | #[unstable(feature = "foobar", issue = "1", implied_by = "bar")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// aux-build:stability-attribute-implies.rs
2+
3+
// Tests that despite the `foobar` feature being implied by now-stable feature `foo`, if `foobar`
4+
// isn't allowed in this crate then an error will be emitted.
5+
6+
extern crate stability_attribute_implies;
7+
use stability_attribute_implies::{foo, foobar};
8+
//~^ ERROR use of unstable library feature 'foobar'
9+
10+
fn main() {
11+
foo(); // no error - stable
12+
foobar(); //~ ERROR use of unstable library feature 'foobar'
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: use of unstable library feature 'foobar'
2+
--> $DIR/stability-attribute-implies-no-feature.rs:7:40
3+
|
4+
LL | use stability_attribute_implies::{foo, foobar};
5+
| ^^^^^^
6+
|
7+
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
8+
= help: add `#![feature(foobar)]` to the crate attributes to enable
9+
10+
error[E0658]: use of unstable library feature 'foobar'
11+
--> $DIR/stability-attribute-implies-no-feature.rs:12:5
12+
|
13+
LL | foobar();
14+
| ^^^^^^
15+
|
16+
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
17+
= help: add `#![feature(foobar)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aux-build:stability-attribute-implies.rs
2+
#![deny(stable_features)]
3+
#![feature(foo)]
4+
//~^ ERROR the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
5+
6+
// Tests that the use of `implied_by` in the `#[unstable]` attribute results in a diagnostic
7+
// mentioning partial stabilization, and that given the implied unstable feature is unused (there
8+
// is no `foobar` call), that the compiler suggests removing the flag.
9+
10+
extern crate stability_attribute_implies;
11+
use stability_attribute_implies::foo;
12+
13+
fn main() {
14+
foo();
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
2+
--> $DIR/stability-attribute-implies-using-stable.rs:3:12
3+
|
4+
LL | #![feature(foo)]
5+
| ^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/stability-attribute-implies-using-stable.rs:2:9
9+
|
10+
LL | #![deny(stable_features)]
11+
| ^^^^^^^^^^^^^^^
12+
help: if you are using features which are still unstable, change to using `foobar`
13+
|
14+
LL | #![feature(foobar)]
15+
| ~~~~~~
16+
help: if you are using features which are now stable, remove this line
17+
|
18+
LL - #![feature(foo)]
19+
|
20+
21+
error: aborting due to previous error
22+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// aux-build:stability-attribute-implies.rs
2+
#![deny(stable_features)]
3+
#![feature(foo)]
4+
//~^ ERROR the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
5+
6+
// Tests that the use of `implied_by` in the `#[unstable]` attribute results in a diagnostic
7+
// mentioning partial stabilization and that given the implied unstable feature is used (there is a
8+
// `foobar` call), that the compiler suggests changing to that feature and doesn't error about its
9+
// use.
10+
11+
extern crate stability_attribute_implies;
12+
use stability_attribute_implies::{foo, foobar};
13+
14+
fn main() {
15+
foo();
16+
foobar(); // no error!
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
2+
--> $DIR/stability-attribute-implies-using-unstable.rs:3:12
3+
|
4+
LL | #![feature(foo)]
5+
| ^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/stability-attribute-implies-using-unstable.rs:2:9
9+
|
10+
LL | #![deny(stable_features)]
11+
| ^^^^^^^^^^^^^^^
12+
help: if you are using features which are still unstable, change to using `foobar`
13+
|
14+
LL | #![feature(foobar)]
15+
| ~~~~~~
16+
help: if you are using features which are now stable, remove this line
17+
|
18+
LL - #![feature(foo)]
19+
|
20+
21+
error: aborting due to previous error
22+

‎src/test/ui/stability-attribute/stability-attribute-sanity-2.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0541]: unknown meta item 'sinse'
88
--> $DIR/stability-attribute-sanity-2.rs:10:25
99
|
1010
LL | #[stable(feature = "a", sinse = "1.0.0")]
11-
| ^^^^^^^^^^^^^^^ expected one of `since`, `note`
11+
| ^^^^^^^^^^^^^^^ expected one of `feature`, `since`
1212

1313
error[E0545]: `issue` must be a non-zero numeric string or "none"
1414
--> $DIR/stability-attribute-sanity-2.rs:13:27

‎src/test/ui/stability-attribute/stability-attribute-sanity.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0541]: unknown meta item 'reason'
1414
--> $DIR/stability-attribute-sanity.rs:8:42
1515
|
1616
LL | #[stable(feature = "a", since = "b", reason)]
17-
| ^^^^^^ expected one of `since`, `note`
17+
| ^^^^^^ expected one of `feature`, `since`
1818

1919
error[E0539]: incorrect meta item
2020
--> $DIR/stability-attribute-sanity.rs:11:29

0 commit comments

Comments
 (0)
Please sign in to comment.