Skip to content

Commit 6246d66

Browse files
committed
passes: improved partial stabilization diagnostic
Improves the diagnostic when a feature attribute is specified unnecessarily but the feature implies another (i.e. it was partially stabilized) to refer to the implied feature. Signed-off-by: David Wood <[email protected]>
1 parent 97edb9f commit 6246d66

File tree

11 files changed

+115
-14
lines changed

11 files changed

+115
-14
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+7
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

+3
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

+15
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

+1
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/stability.rs

+13
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 {

compiler/rustc_middle/src/query/mod.rs

+6-2
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/stability.rs

+50-8
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};
5+
use rustc_attr::{self as attr, ConstStability, Stability, Unstable};
66
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
7-
use rustc_errors::struct_span_err;
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};
@@ -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
{
@@ -668,6 +673,7 @@ pub(crate) fn provide(providers: &mut Providers) {
668673
*providers = Providers {
669674
check_mod_unstable_api_usage,
670675
stability_index,
676+
stability_implications: |tcx, _| tcx.stability().implications.clone(),
671677
lookup_stability: |tcx, id| tcx.stability().local_stability(id.expect_local()),
672678
lookup_const_stability: |tcx, id| tcx.stability().local_const_stability(id.expect_local()),
673679
lookup_deprecation_entry: |tcx, id| {
@@ -946,11 +952,18 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
946952
remaining_lib_features.remove(&sym::libc);
947953
remaining_lib_features.remove(&sym::test);
948954

955+
let mut implications = tcx.stability_implications(rustc_hir::def_id::LOCAL_CRATE).clone();
956+
for &cnum in tcx.crates(()) {
957+
implications.extend(tcx.stability_implications(cnum));
958+
}
959+
949960
let check_features = |remaining_lib_features: &mut FxIndexMap<_, _>, defined_features: &[_]| {
950961
for &(feature, since) in defined_features {
951-
if let Some(since) = since {
952-
if let Some(span) = remaining_lib_features.get(&feature) {
953-
// Warn if the user has enabled an already-stable lib feature.
962+
if let Some(since) = since && let Some(span) = remaining_lib_features.get(&feature) {
963+
// Warn if the user has enabled an already-stable lib feature.
964+
if let Some(implies) = implications.get(&feature) {
965+
unnecessary_partially_stable_feature_lint(tcx, *span, feature, *implies, since);
966+
} else {
954967
unnecessary_stable_feature_lint(tcx, *span, feature, since);
955968
}
956969
}
@@ -983,12 +996,41 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
983996
// don't lint about unused features. We should re-enable this one day!
984997
}
985998

999+
fn unnecessary_partially_stable_feature_lint(
1000+
tcx: TyCtxt<'_>,
1001+
span: Span,
1002+
feature: Symbol,
1003+
implies: Symbol,
1004+
since: Symbol,
1005+
) {
1006+
tcx.struct_span_lint_hir(lint::builtin::STABLE_FEATURES, hir::CRATE_HIR_ID, span, |lint| {
1007+
lint.build(&format!(
1008+
"the feature `{feature}` has been partially stabilized since {since} and is succeeded \
1009+
by the feature `{implies}`"
1010+
))
1011+
.span_suggestion(
1012+
span,
1013+
&format!(
1014+
"if you are using features which are still unstable, change to using `{implies}`"
1015+
),
1016+
implies,
1017+
Applicability::MaybeIncorrect,
1018+
)
1019+
.span_suggestion(
1020+
tcx.sess.source_map().span_extend_to_line(span),
1021+
"if you are using features which are now stable, remove this line",
1022+
"",
1023+
Applicability::MaybeIncorrect,
1024+
)
1025+
.emit();
1026+
});
1027+
}
1028+
9861029
fn unnecessary_stable_feature_lint(tcx: TyCtxt<'_>, span: Span, feature: Symbol, since: Symbol) {
9871030
tcx.struct_span_lint_hir(lint::builtin::STABLE_FEATURES, hir::CRATE_HIR_ID, span, |lint| {
9881031
lint.build(&format!(
989-
"the feature `{}` has been stable since {} and no longer requires \
990-
an attribute to enable",
991-
feature, since
1032+
"the feature `{feature}` has been stable since {since} and no longer requires an \
1033+
attribute to enable",
9921034
))
9931035
.emit();
9941036
});

src/test/ui/stability-attribute/stability-attribute-implies-using-stable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// aux-build:stability-attribute-implies.rs
22
#![deny(stable_features)]
33
#![feature(foo)]
4-
//~^ ERROR the feature `foo` has been stable since 1.62.0 and no longer requires an attribute to enable
4+
//~^ ERROR the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
55

66
// Tests that the use of `implied_by` in the `#[unstable]` attribute results in a diagnostic
77
// mentioning partial stabilization, and that given the implied unstable feature is unused (there

src/test/ui/stability-attribute/stability-attribute-implies-using-stable.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the feature `foo` has been stable since 1.62.0 and no longer requires an attribute to enable
1+
error: the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
22
--> $DIR/stability-attribute-implies-using-stable.rs:3:12
33
|
44
LL | #![feature(foo)]
@@ -9,6 +9,14 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(stable_features)]
1111
| ^^^^^^^^^^^^^^^
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+
|
1220

1321
error: aborting due to previous error
1422

src/test/ui/stability-attribute/stability-attribute-implies-using-unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// aux-build:stability-attribute-implies.rs
22
#![deny(stable_features)]
33
#![feature(foo)]
4-
//~^ ERROR the feature `foo` has been stable since 1.62.0 and no longer requires an attribute to enable
4+
//~^ ERROR the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
55

66
// Tests that the use of `implied_by` in the `#[unstable]` attribute results in a diagnostic
77
// mentioning partial stabilization and that given the implied unstable feature is used (there is a

src/test/ui/stability-attribute/stability-attribute-implies-using-unstable.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the feature `foo` has been stable since 1.62.0 and no longer requires an attribute to enable
1+
error: the feature `foo` has been partially stabilized since 1.62.0 and is succeeded by the feature `foobar`
22
--> $DIR/stability-attribute-implies-using-unstable.rs:3:12
33
|
44
LL | #![feature(foo)]
@@ -9,6 +9,14 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(stable_features)]
1111
| ^^^^^^^^^^^^^^^
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+
|
1220

1321
error: aborting due to previous error
1422

0 commit comments

Comments
 (0)