Skip to content

Commit 6c8265d

Browse files
committed
only_local: always check for misuse
1 parent fc128b6 commit 6c8265d

File tree

36 files changed

+341
-380
lines changed

36 files changed

+341
-380
lines changed

compiler/rustc_attr/src/builtin.rs

+141-138
Large diffs are not rendered by default.

compiler/rustc_codegen_llvm/src/attributes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::def_id::DefId;
66
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
77
use rustc_middle::ty::{self, TyCtxt};
88
use rustc_session::config::OptLevel;
9+
use rustc_span::symbol::sym;
910
use rustc_target::spec::abi::Abi;
1011
use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
1112
use smallvec::SmallVec;
@@ -329,9 +330,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
329330
) {
330331
let span = cx
331332
.tcx
332-
.get_attrs(instance.def_id())
333-
.iter()
334-
.find(|a| a.has_name(rustc_span::sym::target_feature))
333+
.get_attr(instance.def_id(), sym::target_feature)
335334
.map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
336335
let msg = format!(
337336
"the target features {} must all be either enabled or disabled together",

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
312312

313313
Status::Unstable(gate) if self.tcx.features().enabled(gate) => {
314314
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
315-
&& !super::rustc_allow_const_fn_unstable(
316-
self.tcx,
317-
self.def_id().to_def_id(),
318-
gate,
319-
);
315+
&& !super::rustc_allow_const_fn_unstable(self.tcx, self.def_id(), gate);
320316
if unstable_in_stable {
321317
emit_unstable_in_stable_error(self.ccx, span, gate);
322318
}
@@ -713,7 +709,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
713709
match &terminator.kind {
714710
TerminatorKind::Call { func, args, fn_span, from_hir_call, .. } => {
715711
let ConstCx { tcx, body, param_env, .. } = *self.ccx;
716-
let caller = self.def_id().to_def_id();
712+
let caller = self.def_id();
717713

718714
let fn_ty = func.ty(body, tcx);
719715

@@ -797,7 +793,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
797793
// trait.
798794
let callee_trait = tcx.trait_of_item(callee);
799795
if callee_trait.is_some()
800-
&& tcx.has_attr(caller, sym::default_method_body_is_const)
796+
&& tcx.has_attr(caller.to_def_id(), sym::default_method_body_is_const)
801797
&& callee_trait == tcx.trait_of_item(caller)
802798
// Can only call methods when it's `<Self as TheTrait>::f`.
803799
&& tcx.types.self_param == substs.type_at(0)

compiler/rustc_const_eval/src/transform/check_consts/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
6666
}
6767
}
6868

69-
pub fn rustc_allow_const_fn_unstable(tcx: TyCtxt<'_>, def_id: DefId, feature_gate: Symbol) -> bool {
70-
let attrs = tcx.get_attrs(def_id);
69+
pub fn rustc_allow_const_fn_unstable(
70+
tcx: TyCtxt<'_>,
71+
def_id: LocalDefId,
72+
feature_gate: Symbol,
73+
) -> bool {
74+
let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
7175
attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs).any(|name| name == feature_gate)
7276
}
7377

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
3+
use hir::def_id::LocalDefId;
34
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
45
use rustc_hir as hir;
56
use rustc_hir::def_id::DefId;
@@ -95,7 +96,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
9596
/// A function call where the callee is not marked as `const`.
9697
#[derive(Debug, Clone, Copy)]
9798
pub struct FnCallNonConst<'tcx> {
98-
pub caller: DefId,
99+
pub caller: LocalDefId,
99100
pub callee: DefId,
100101
pub substs: SubstsRef<'tcx>,
101102
pub span: Span,
@@ -117,13 +118,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
117118
match self_ty.kind() {
118119
Param(param_ty) => {
119120
debug!(?param_ty);
120-
if let Some(generics) = caller
121-
.as_local()
122-
.map(|id| tcx.hir().local_def_id_to_hir_id(id))
123-
.map(|id| tcx.hir().get(id))
124-
.as_ref()
125-
.and_then(|node| node.generics())
126-
{
121+
let caller_hir_id = tcx.hir().local_def_id_to_hir_id(caller);
122+
if let Some(generics) = tcx.hir().get(caller_hir_id).generics() {
127123
let constraint = with_no_trimmed_paths!(format!(
128124
"~const {}",
129125
trait_ref.print_only_trait_path()

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
377377
ungated!(panic_handler, Normal, template!(Word), WarnFollowing), // RFC 2070
378378

379379
// Code generation:
380-
ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing, @only_local: true),
380+
ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing),
381381
ungated!(cold, Normal, template!(Word), WarnFollowing),
382382
ungated!(no_builtins, CrateLevel, template!(Word), WarnFollowing),
383383
ungated!(target_feature, Normal, template!(List: r#"enable = "name""#), DuplicatesOk),

compiler/rustc_incremental/src/persist/dirty_clean.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ pub struct DirtyCleanVisitor<'tcx> {
183183
impl<'tcx> DirtyCleanVisitor<'tcx> {
184184
/// Possibly "deserialize" the attribute into a clean/dirty assertion
185185
fn assertion_maybe(&mut self, item_id: LocalDefId, attr: &Attribute) -> Option<Assertion> {
186-
if !attr.has_name(sym::rustc_clean) {
187-
// skip: not rustc_clean/dirty
188-
return None;
189-
}
186+
assert!(attr.has_name(sym::rustc_clean));
190187
if !check_config(self.tcx, attr) {
191188
// skip: not the correct `cfg=`
192189
return None;
@@ -384,7 +381,7 @@ impl<'tcx> DirtyCleanVisitor<'tcx> {
384381
fn check_item(&mut self, item_id: LocalDefId) {
385382
let item_span = self.tcx.def_span(item_id.to_def_id());
386383
let def_path_hash = self.tcx.def_path_hash(item_id.to_def_id());
387-
for attr in self.tcx.get_attrs(item_id.to_def_id()).iter() {
384+
for attr in self.tcx.get_attrs(item_id.to_def_id(), sym::rustc_clean) {
388385
let Some(assertion) = self.assertion_maybe(item_id, attr) else {
389386
continue;
390387
};

compiler/rustc_interface/src/queries.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ impl<'tcx> Queries<'tcx> {
258258
/// an error.
259259
fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
260260
let Some((def_id, _)) = tcx.entry_fn(()) else { return };
261-
262-
let attrs = &*tcx.get_attrs(def_id);
263-
let attrs = attrs.iter().filter(|attr| attr.has_name(sym::rustc_error));
264-
for attr in attrs {
261+
for attr in tcx.get_attrs(def_id, sym::rustc_error) {
265262
match attr.meta_item_list() {
266263
// Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
267264
Some(list)

compiler/rustc_lint/src/builtin.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl MissingDoc {
551551
}
552552
}
553553

554-
let attrs = cx.tcx.get_attrs(def_id.to_def_id());
554+
let attrs = cx.tcx.hir().attrs(cx.tcx.hir().local_def_id_to_hir_id(def_id));
555555
let has_doc = attrs.iter().any(has_doc);
556556
if !has_doc {
557557
cx.struct_span_lint(
@@ -2737,11 +2737,7 @@ impl ClashingExternDeclarations {
27372737
// bottleneck, this does just fine.
27382738
(
27392739
overridden_link_name,
2740-
tcx.get_attrs(fi.def_id.to_def_id())
2741-
.iter()
2742-
.find(|at| at.has_name(sym::link_name))
2743-
.unwrap()
2744-
.span,
2740+
tcx.get_attr(fi.def_id.to_def_id(), sym::link_name).unwrap().span,
27452741
)
27462742
})
27472743
{

compiler/rustc_lint/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ enum FfiResult<'tcx> {
668668
}
669669

670670
crate fn nonnull_optimization_guaranteed<'tcx>(tcx: TyCtxt<'tcx>, def: ty::AdtDef<'tcx>) -> bool {
671-
tcx.get_attrs(def.did()).iter().any(|a| a.has_name(sym::rustc_nonnull_optimization_guaranteed))
671+
tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
672672
}
673673

674674
/// `repr(transparent)` structs can have a single non-ZST field, this function returns that

compiler/rustc_lint/src/unused.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -303,26 +303,25 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
303303
descr_pre_path: &str,
304304
descr_post_path: &str,
305305
) -> bool {
306-
for attr in cx.tcx.get_attrs(def_id).iter() {
307-
if attr.has_name(sym::must_use) {
308-
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
309-
let msg = format!(
310-
"unused {}`{}`{} that must be used",
311-
descr_pre_path,
312-
cx.tcx.def_path_str(def_id),
313-
descr_post_path
314-
);
315-
let mut err = lint.build(&msg);
316-
// check for #[must_use = "..."]
317-
if let Some(note) = attr.value_str() {
318-
err.note(note.as_str());
319-
}
320-
err.emit();
321-
});
322-
return true;
323-
}
306+
if let Some(attr) = cx.tcx.get_attr(def_id, sym::must_use) {
307+
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
308+
let msg = format!(
309+
"unused {}`{}`{} that must be used",
310+
descr_pre_path,
311+
cx.tcx.def_path_str(def_id),
312+
descr_post_path
313+
);
314+
let mut err = lint.build(&msg);
315+
// check for #[must_use = "..."]
316+
if let Some(note) = attr.value_str() {
317+
err.note(note.as_str());
318+
}
319+
err.emit();
320+
});
321+
true
322+
} else {
323+
false
324324
}
325-
false
326325
}
327326
}
328327
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -985,15 +985,17 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
985985
}
986986

987987
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
988-
fn encode_attrs(&mut self, def_id: DefId) {
988+
fn encode_attrs(&mut self, def_id: LocalDefId) {
989989
let mut attrs = self
990990
.tcx
991-
.get_attrs(def_id)
991+
.hir()
992+
.attrs(self.tcx.hir().local_def_id_to_hir_id(def_id))
992993
.iter()
993994
.filter(|attr| !rustc_feature::is_builtin_only_local(attr.name_or_empty()));
994-
record!(self.tables.attributes[def_id] <- attrs.clone());
995+
996+
record!(self.tables.attributes[def_id.to_def_id()] <- attrs.clone());
995997
if attrs.any(|attr| attr.may_have_doc_links()) {
996-
self.tables.may_have_doc_links.set(def_id.index, ());
998+
self.tables.may_have_doc_links.set(def_id.local_def_index, ());
997999
}
9981000
}
9991001

@@ -1009,7 +1011,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10091011
let Some(def_kind) = def_kind else { continue };
10101012
self.tables.opt_def_kind.set(def_id.index, def_kind);
10111013
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
1012-
self.encode_attrs(def_id);
1014+
self.encode_attrs(local_id);
10131015
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
10141016
if def_kind.has_codegen_attrs() {
10151017
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
@@ -1674,7 +1676,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16741676

16751677
self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
16761678
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
1677-
self.encode_attrs(LOCAL_CRATE.as_def_id());
1679+
self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
16781680
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id()));
16791681
if let Some(stability) = stability {
16801682
record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
@@ -1715,7 +1717,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17151717
let def_id = id.to_def_id();
17161718
self.tables.opt_def_kind.set(def_id.index, DefKind::Macro(macro_kind));
17171719
record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind));
1718-
self.encode_attrs(def_id);
1720+
self.encode_attrs(id);
17191721
record!(self.tables.def_keys[def_id] <- def_key);
17201722
record!(self.tables.def_ident_span[def_id] <- span);
17211723
record!(self.tables.def_span[def_id] <- span);

compiler/rustc_middle/src/ty/adt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ impl AdtDefData {
230230
flags |= AdtFlags::HAS_CTOR;
231231
}
232232

233-
let attrs = tcx.get_attrs(did);
234-
if tcx.sess.contains_name(&attrs, sym::fundamental) {
233+
if tcx.has_attr(did, sym::fundamental) {
235234
flags |= AdtFlags::IS_FUNDAMENTAL;
236235
}
237236
if Some(did) == tcx.lang_items().phantom_data() {

compiler/rustc_middle/src/ty/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,8 @@ impl<'tcx> TyCtxt<'tcx> {
11481148
/// `rustc_layout_scalar_valid_range` attribute.
11491149
// FIXME(eddyb) this is an awkward spot for this method, maybe move it?
11501150
pub fn layout_scalar_valid_range(self, def_id: DefId) -> (Bound<u128>, Bound<u128>) {
1151-
let attrs = self.get_attrs(def_id);
11521151
let get = |name| {
1153-
let Some(attr) = attrs.iter().find(|a| a.has_name(name)) else {
1152+
let Some(attr) = self.get_attr(def_id, name) else {
11541153
return Bound::Unbounded;
11551154
};
11561155
debug!("layout_scalar_valid_range: attr={:?}", attr);

compiler/rustc_middle/src/ty/error.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,8 @@ impl<T> Trait<T> for X {
568568
}
569569
}
570570
TargetFeatureCast(def_id) => {
571-
let attrs = self.get_attrs(*def_id);
572-
let target_spans = attrs
573-
.iter()
574-
.filter(|attr| attr.has_name(sym::target_feature))
575-
.map(|attr| attr.span);
571+
let target_spans =
572+
self.get_attrs(*def_id, sym::target_feature).map(|attr| attr.span);
576573
diag.note(
577574
"functions with `#[target_feature]` can only be coerced to `unsafe` function pointers"
578575
);

compiler/rustc_middle/src/ty/instance.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::def_id::{CrateNum, DefId};
88
use rustc_hir::lang_items::LangItem;
99
use rustc_macros::HashStable;
1010
use rustc_middle::ty::normalize_erasing_regions::NormalizationError;
11+
use rustc_span::Symbol;
1112

1213
use std::fmt;
1314

@@ -185,8 +186,8 @@ impl<'tcx> InstanceDef<'tcx> {
185186
}
186187

187188
#[inline]
188-
pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
189-
tcx.get_attrs(self.def_id())
189+
pub fn get_attrs(&self, tcx: TyCtxt<'tcx>, attr: Symbol) -> ty::Attributes<'tcx> {
190+
tcx.get_attrs(self.def_id(), attr)
190191
}
191192

192193
/// Returns `true` if the LLVM version of this instance is unconditionally

0 commit comments

Comments
 (0)