Skip to content

Commit 5d925fe

Browse files
Auto merge of #144841 - cjgillot:typeck-no-attrs, r=<try>
Access less HIR attributes from typeck
2 parents 0060d5a + 5073886 commit 5d925fe

File tree

7 files changed

+35
-35
lines changed

7 files changed

+35
-35
lines changed

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
511511
// Untranslatable diagnostics are okay for rustc internals
512512
#[allow(rustc::untranslatable_diagnostic)]
513513
#[allow(rustc::diagnostic_outside_of_impl)]
514-
if self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses) {
514+
if self.has_rustc_attrs
515+
&& self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses)
516+
{
515517
let predicates = self.tcx.predicates_of(def_id);
516518
let predicates = predicates.instantiate(self.tcx, args);
517519
for (predicate, predicate_span) in predicates {
@@ -894,7 +896,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
894896
}
895897

896898
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
897-
if self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
899+
if self.has_rustc_attrs && self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
898900
return;
899901
}
900902

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ pub(crate) struct FnCtxt<'a, 'tcx> {
126126
/// These are stored here so we may collect them when canonicalizing user
127127
/// type ascriptions later.
128128
pub(super) trait_ascriptions: RefCell<ItemLocalMap<Vec<ty::Clause<'tcx>>>>,
129+
130+
/// Whether the current crate enables the `rustc_attrs` feature.
131+
/// This allows to skip processing attributes in many places.
132+
pub(super) has_rustc_attrs: bool,
129133
}
130134

131135
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -154,6 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
154158
diverging_fallback_behavior,
155159
diverging_block_behavior,
156160
trait_ascriptions: Default::default(),
161+
has_rustc_attrs: root_ctxt.tcx.features().rustc_attrs(),
157162
}
158163
}
159164

@@ -525,10 +530,13 @@ fn parse_never_type_options_attr(
525530
let mut fallback = None;
526531
let mut block = None;
527532

528-
let items = tcx
529-
.get_attr(CRATE_DEF_ID, sym::rustc_never_type_options)
530-
.map(|attr| attr.meta_item_list().unwrap())
531-
.unwrap_or_default();
533+
let items = if tcx.features().rustc_attrs() {
534+
tcx.get_attr(CRATE_DEF_ID, sym::rustc_never_type_options)
535+
.map(|attr| attr.meta_item_list().unwrap())
536+
} else {
537+
None
538+
};
539+
let items = items.unwrap_or_default();
532540

533541
for item in items {
534542
if item.has_name(sym::fallback) && fallback.is_none() {

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ use rustc_data_structures::unord::UnordSet;
4646
use rustc_errors::codes::*;
4747
use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
4848
use rustc_hir as hir;
49-
use rustc_hir::attrs::AttributeKind;
5049
use rustc_hir::def::{DefKind, Res};
51-
use rustc_hir::{HirId, HirIdMap, Node, find_attr};
50+
use rustc_hir::{HirId, HirIdMap, Node};
5251
use rustc_hir_analysis::check::{check_abi, check_custom_abi};
5352
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
5453
use rustc_infer::traits::{ObligationCauseCode, ObligationInspector, WellFormedLoc};
54+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5555
use rustc_middle::query::Providers;
5656
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
5757
use rustc_middle::{bug, span_bug};
@@ -174,7 +174,7 @@ fn typeck_with_inspect<'tcx>(
174174
.map(|(idx, ty)| fcx.normalize(arg_span(idx), ty)),
175175
);
176176

177-
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Naked(..)) {
177+
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NAKED) {
178178
naked_functions::typeck_naked_fn(tcx, def_id, body);
179179
}
180180

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -777,31 +777,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
777777

778778
self.assemble_inherent_candidates_from_object(generalized_self_ty);
779779
self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
780-
if self.tcx.has_attr(p.def_id(), sym::rustc_has_incoherent_inherent_impls) {
781-
self.assemble_inherent_candidates_for_incoherent_ty(
782-
raw_self_ty,
783-
receiver_steps,
784-
);
785-
}
780+
self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
786781
}
787782
ty::Adt(def, _) => {
788783
let def_id = def.did();
789784
self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
790-
if self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
791-
self.assemble_inherent_candidates_for_incoherent_ty(
792-
raw_self_ty,
793-
receiver_steps,
794-
);
795-
}
785+
self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
796786
}
797787
ty::Foreign(did) => {
798788
self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
799-
if self.tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
800-
self.assemble_inherent_candidates_for_incoherent_ty(
801-
raw_self_ty,
802-
receiver_steps,
803-
);
804-
}
789+
self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
805790
}
806791
ty::Param(_) => {
807792
self.assemble_inherent_candidates_from_param(raw_self_ty);
@@ -2373,17 +2358,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
23732358
if !self.is_relevant_kind_for_mode(x.kind) {
23742359
return false;
23752360
}
2376-
if self.matches_by_doc_alias(x.def_id) {
2377-
return true;
2378-
}
2379-
match edit_distance_with_substrings(
2361+
if let Some(d) = edit_distance_with_substrings(
23802362
name.as_str(),
23812363
x.name().as_str(),
23822364
max_dist,
23832365
) {
2384-
Some(d) => d > 0,
2385-
None => false,
2366+
return d > 0;
23862367
}
2368+
self.matches_by_doc_alias(x.def_id)
23872369
})
23882370
.copied()
23892371
.collect()

compiler/rustc_hir_typeck/src/upvar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17471747
}
17481748

17491749
fn should_log_capture_analysis(&self, closure_def_id: LocalDefId) -> bool {
1750-
self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis)
1750+
self.has_rustc_attrs && self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis)
17511751
}
17521752

17531753
fn log_capture_analysis_first_pass(

compiler/rustc_hir_typeck/src/writeback.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4545

4646
// This attribute causes us to dump some writeback information
4747
// in the form of errors, which is used for unit tests.
48-
let rustc_dump_user_args = self.tcx.has_attr(item_def_id, sym::rustc_dump_user_args);
48+
let rustc_dump_user_args =
49+
self.has_rustc_attrs && self.tcx.has_attr(item_def_id, sym::rustc_dump_user_args);
4950

5051
let mut wbcx = WritebackCx::new(self, body, rustc_dump_user_args);
5152
for param in body.params {

compiler/rustc_middle/src/ty/trait_def.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir as hir;
66
use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
88
use rustc_macros::{Decodable, Encodable, HashStable};
9+
use rustc_span::symbol::sym;
910
use tracing::debug;
1011

1112
use crate::query::LocalCrate;
@@ -239,6 +240,12 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
239240

240241
/// Query provider for `incoherent_impls`.
241242
pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
243+
if let Some(def_id) = simp.def()
244+
&& !tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls)
245+
{
246+
return &[];
247+
}
248+
242249
let mut impls = Vec::new();
243250
for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
244251
for &impl_def_id in tcx.crate_incoherent_impls((cnum, simp)) {

0 commit comments

Comments
 (0)