1
Fork 0

Auto merge of #130036 - weiznich:diagnostic_unstable_tracking, r=compiler-errors

Correctly handle stability of `#[diagnostic]` attributes

This commit changes the way we treat the stability of attributes in the
`#[diagnostic]` namespace. Instead of relaying on ad-hoc checks to
ensure at call side that a certain attribute is really usable at that
location it centralises the logic to one place. For diagnostic
attributes comming from other crates it just skips serializing
attributes that are not stable and that do not have the corresponding
feature enabled. For attributes from the current crate we can just use
the feature information provided by `TyCtx`.

r​? `@compiler-errors`
This commit is contained in:
bors 2024-09-08 23:39:00 +00:00
commit 085744b7ad
6 changed files with 51 additions and 9 deletions

View file

@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::{Mmap, MmapMut};
use rustc_data_structures::sync::{join, par_for_each_in, Lrc};
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_feature::Features;
use rustc_hir as hir;
use rustc_hir::def_id::{LocalDefId, LocalDefIdSet, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
@ -797,9 +798,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}
struct AnalyzeAttrState {
struct AnalyzeAttrState<'a> {
is_exported: bool,
is_doc_hidden: bool,
features: &'a Features,
}
/// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and
@ -812,7 +814,7 @@ struct AnalyzeAttrState {
/// visibility: this is a piece of data that can be computed once per defid, and not once per
/// attribute. Some attributes would only be usable downstream if they are public.
#[inline]
fn analyze_attr(attr: &Attribute, state: &mut AnalyzeAttrState) -> bool {
fn analyze_attr(attr: &Attribute, state: &mut AnalyzeAttrState<'_>) -> bool {
let mut should_encode = false;
if !rustc_feature::encode_cross_crate(attr.name_or_empty()) {
// Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
@ -837,6 +839,9 @@ fn analyze_attr(attr: &Attribute, state: &mut AnalyzeAttrState) -> bool {
}
}
}
} else if attr.path().starts_with(&[sym::diagnostic]) && attr.path().len() == 2 {
should_encode =
rustc_feature::is_stable_diagnostic_attribute(attr.path()[1], state.features);
} else {
should_encode = true;
}
@ -1343,6 +1348,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let mut state = AnalyzeAttrState {
is_exported: tcx.effective_visibilities(()).is_exported(def_id),
is_doc_hidden: false,
features: &tcx.features(),
};
let attr_iter = tcx
.hir()