Skip to content

Commit f0954c5

Browse files
authored
Unrolled build for #142687
Rollup merge of #142687 - cjgillot:less-hir_crate, r=oli-obk Reduce uses of `hir_crate`. I tried rebasing my old incremental-HIR branch. This is a by-product, which is required if we want to get rid of `hir_crate` entirely. The second commit is a drive-by cleanup. It can be pulled into its own PR. r? ````@oli-obk````
2 parents 18491d5 + ede4891 commit f0954c5

File tree

16 files changed

+48
-102
lines changed

16 files changed

+48
-102
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
463463
);
464464
}
465465

466-
fn crate_inject_span(&self) -> Option<Span> {
467-
self.tcx.hir_crate_items(()).definitions().next().and_then(|id| {
468-
self.tcx.crate_level_attribute_injection_span(self.tcx.local_def_id_to_hir_id(id))
469-
})
470-
}
471-
472466
/// Check the const stability of the given item (fn or trait).
473467
fn check_callee_stability(&mut self, def_id: DefId) {
474468
match self.tcx.lookup_const_stability(def_id) {
@@ -543,7 +537,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
543537
feature,
544538
feature_enabled,
545539
safe_to_expose_on_stable: callee_safe_to_expose_on_stable,
546-
suggestion_span: self.crate_inject_span(),
547540
is_function_call: self.tcx.def_kind(def_id) != DefKind::Trait,
548541
});
549542
}
@@ -919,7 +912,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
919912
name: intrinsic.name,
920913
feature,
921914
const_stable_indirect: is_const_stable,
922-
suggestion: self.crate_inject_span(),
923915
});
924916
}
925917
Some(attrs::ConstStability {

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::{ConstContext, LangItem};
4+
use rustc_errors::Diag;
45
use rustc_errors::codes::*;
5-
use rustc_errors::{Applicability, Diag};
66
use rustc_hir as hir;
77
use rustc_hir::def_id::DefId;
88
use rustc_infer::infer::TyCtxtInferExt;
@@ -384,7 +384,6 @@ pub(crate) struct CallUnstable {
384384
/// expose on stable.
385385
pub feature_enabled: bool,
386386
pub safe_to_expose_on_stable: bool,
387-
pub suggestion_span: Option<Span>,
388387
/// true if `def_id` is the function we are calling, false if `def_id` is an unstable trait.
389388
pub is_function_call: bool,
390389
}
@@ -412,20 +411,7 @@ impl<'tcx> NonConstOp<'tcx> for CallUnstable {
412411
def_path: ccx.tcx.def_path_str(self.def_id),
413412
})
414413
};
415-
// FIXME: make this translatable
416-
let msg = format!("add `#![feature({})]` to the crate attributes to enable", self.feature);
417-
#[allow(rustc::untranslatable_diagnostic)]
418-
if let Some(span) = self.suggestion_span {
419-
err.span_suggestion_verbose(
420-
span,
421-
msg,
422-
format!("#![feature({})]\n", self.feature),
423-
Applicability::MachineApplicable,
424-
);
425-
} else {
426-
err.help(msg);
427-
}
428-
414+
ccx.tcx.disabled_nightly_features(&mut err, [(String::new(), self.feature)]);
429415
err
430416
}
431417
}
@@ -452,7 +438,6 @@ pub(crate) struct IntrinsicUnstable {
452438
pub name: Symbol,
453439
pub feature: Symbol,
454440
pub const_stable_indirect: bool,
455-
pub suggestion: Option<Span>,
456441
}
457442

458443
impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
@@ -472,8 +457,7 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
472457
span,
473458
name: self.name,
474459
feature: self.feature,
475-
suggestion: self.suggestion,
476-
help: self.suggestion.is_none(),
460+
suggestion: ccx.tcx.crate_level_attribute_injection_span(),
477461
})
478462
}
479463
}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ pub(crate) struct UnstableIntrinsic {
136136
code = "#![feature({feature})]\n",
137137
applicability = "machine-applicable"
138138
)]
139-
pub suggestion: Option<Span>,
140-
#[help(const_eval_unstable_intrinsic_suggestion)]
141-
pub help: bool,
139+
pub suggestion: Span,
142140
}
143141

144142
#[derive(Diagnostic)]

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
292292
}
293293
HirTree => {
294294
debug!("pretty printing HIR tree");
295-
format!("{:#?}", ex.tcx().hir_crate(()))
295+
ex.tcx()
296+
.hir_crate_items(())
297+
.owners()
298+
.map(|owner| format!("{:#?} => {:#?}\n", owner, ex.tcx().hir_owner_nodes(owner)))
299+
.collect()
296300
}
297301
Mir => {
298302
let mut out = Vec::new();

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ fn default_body_is_unstable(
311311
reason: reason_str,
312312
});
313313

314-
let inject_span = item_did
315-
.as_local()
316-
.and_then(|id| tcx.crate_level_attribute_injection_span(tcx.local_def_id_to_hir_id(id)));
314+
let inject_span = item_did.is_local().then(|| tcx.crate_level_attribute_injection_span());
317315
rustc_session::parse::add_feature_diagnostics_for_issue(
318316
&mut err,
319317
&tcx.sess,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
10641064
Ok(..) => Some(vec![(adt_const_params_feature_string, sym::adt_const_params)]),
10651065
};
10661066
if let Some(features) = may_suggest_feature {
1067-
tcx.disabled_nightly_features(&mut diag, Some(param.hir_id), features);
1067+
tcx.disabled_nightly_features(&mut diag, features);
10681068
}
10691069

10701070
Err(diag.emit())

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ pub(crate) struct BaseExpressionDoubleDot {
3030
)]
3131
pub default_field_values_suggestion: Option<Span>,
3232
#[subdiagnostic]
33-
pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
34-
#[subdiagnostic]
3533
pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
3634
#[subdiagnostic]
3735
pub remove_dots: Option<BaseExpressionDoubleDotRemove>,
@@ -61,10 +59,6 @@ pub(crate) struct BaseExpressionDoubleDotAddExpr {
6159
pub span: Span,
6260
}
6361

64-
#[derive(Subdiagnostic)]
65-
#[help(hir_typeck_base_expression_double_dot_enable_default_field_values)]
66-
pub(crate) struct BaseExpressionDoubleDotEnableDefaultFieldValues;
67-
6862
#[derive(Diagnostic)]
6963
#[diag(hir_typeck_field_multiply_specified_in_initializer, code = E0062)]
7064
pub(crate) struct FieldMultiplySpecifiedInInitializer {

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectatio
4343
use crate::coercion::{CoerceMany, DynamicCoerceMany};
4444
use crate::errors::{
4545
AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
46-
BaseExpressionDoubleDotEnableDefaultFieldValues, BaseExpressionDoubleDotRemove,
47-
CantDereference, FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
48-
HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType, NoFieldOnVariant,
49-
ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
46+
BaseExpressionDoubleDotRemove, CantDereference, FieldMultiplySpecifiedInInitializer,
47+
FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType,
48+
NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
5049
TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
5150
};
5251
use crate::{
@@ -2158,26 +2157,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21582157
}
21592158
}
21602159
if !self.tcx.features().default_field_values() {
2161-
let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
2160+
let sugg = self.tcx.crate_level_attribute_injection_span();
21622161
self.dcx().emit_err(BaseExpressionDoubleDot {
21632162
span: span.shrink_to_hi(),
21642163
// We only mention enabling the feature if this is a nightly rustc *and* the
21652164
// expression would make sense with the feature enabled.
21662165
default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
21672166
&& missing_mandatory_fields.is_empty()
21682167
&& !missing_optional_fields.is_empty()
2169-
&& sugg.is_some()
21702168
{
2171-
sugg
2172-
} else {
2173-
None
2174-
},
2175-
default_field_values_help: if self.tcx.sess.is_nightly_build()
2176-
&& missing_mandatory_fields.is_empty()
2177-
&& !missing_optional_fields.is_empty()
2178-
&& sugg.is_none()
2179-
{
2180-
Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
2169+
Some(sugg)
21812170
} else {
21822171
None
21832172
},

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,6 @@ impl<'tcx> Pick<'tcx> {
17271727
}
17281728
tcx.disabled_nightly_features(
17291729
lint,
1730-
Some(scope_expr_id),
17311730
self.unstable_candidates.iter().map(|(candidate, feature)| {
17321731
(format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
17331732
}),

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,8 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10111011

10121012
// Prefetch this to prevent multiple threads from blocking on it later.
10131013
// This is needed since the `hir_id_validator::check_crate` call above is not guaranteed
1014-
// to use `hir_crate`.
1015-
tcx.ensure_done().hir_crate(());
1014+
// to use `hir_crate_items`.
1015+
tcx.ensure_done().hir_crate_items(());
10161016

10171017
let sess = tcx.sess;
10181018
sess.time("misc_checking_1", || {

compiler/rustc_lint/src/expect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_hir::CRATE_OWNER_ID;
32
use rustc_middle::lint::LintExpectation;
43
use rustc_middle::query::Providers;
54
use rustc_middle::ty::TyCtxt;
@@ -18,7 +17,7 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
1817

1918
let mut expectations = Vec::new();
2019

21-
for owner in std::iter::once(CRATE_OWNER_ID).chain(krate.owners()) {
20+
for owner in krate.owners() {
2221
let lints = tcx.shallow_lint_levels_on(owner);
2322
expectations.extend_from_slice(&lints.expectations);
2423
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ impl<'tcx> TyCtxt<'tcx> {
328328
}
329329

330330
/// Returns an iterator of the `DefId`s for all body-owners in this
331-
/// crate. If you would prefer to iterate over the bodies
332-
/// themselves, you can do `self.hir_crate(()).body_ids.iter()`.
331+
/// crate.
333332
#[inline]
334333
pub fn hir_body_owners(self) -> impl Iterator<Item = LocalDefId> {
335334
self.hir_crate_items(()).body_owners.iter().copied()
@@ -396,12 +395,11 @@ impl<'tcx> TyCtxt<'tcx> {
396395
where
397396
V: Visitor<'tcx>,
398397
{
399-
let krate = self.hir_crate(());
400-
for info in krate.owners.iter() {
401-
if let MaybeOwner::Owner(info) = info {
402-
for attrs in info.attrs.map.values() {
403-
walk_list!(visitor, visit_attribute, *attrs);
404-
}
398+
let krate = self.hir_crate_items(());
399+
for owner in krate.owners() {
400+
let attrs = self.hir_attr_map(owner);
401+
for attrs in attrs.map.values() {
402+
walk_list!(visitor, visit_attribute, *attrs);
405403
}
406404
}
407405
V::Result::output()
@@ -1225,6 +1223,7 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> Mod
12251223
..
12261224
} = collector;
12271225
ModuleItems {
1226+
add_root: false,
12281227
submodules: submodules.into_boxed_slice(),
12291228
free_items: items.into_boxed_slice(),
12301229
trait_items: trait_items.into_boxed_slice(),
@@ -1260,6 +1259,7 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
12601259
} = collector;
12611260

12621261
ModuleItems {
1262+
add_root: true,
12631263
submodules: submodules.into_boxed_slice(),
12641264
free_items: items.into_boxed_slice(),
12651265
trait_items: trait_items.into_boxed_slice(),

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
2424
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
2525
#[derive(Debug, HashStable, Encodable, Decodable)]
2626
pub struct ModuleItems {
27+
/// Whether this represents the whole crate, in which case we need to add `CRATE_OWNER_ID` to
28+
/// the iterators if we want to account for the crate root.
29+
add_root: bool,
2730
submodules: Box<[OwnerId]>,
2831
free_items: Box<[ItemId]>,
2932
trait_items: Box<[TraitItemId]>,
@@ -66,9 +69,10 @@ impl ModuleItems {
6669
}
6770

6871
pub fn owners(&self) -> impl Iterator<Item = OwnerId> {
69-
self.free_items
70-
.iter()
71-
.map(|id| id.owner_id)
72+
self.add_root
73+
.then_some(CRATE_OWNER_ID)
74+
.into_iter()
75+
.chain(self.free_items.iter().map(|id| id.owner_id))
7276
.chain(self.trait_items.iter().map(|id| id.owner_id))
7377
.chain(self.impl_items.iter().map(|id| id.owner_id))
7478
.chain(self.foreign_items.iter().map(|id| id.owner_id))

compiler/rustc_middle/src/ty/context.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ impl<'tcx> TyCtxt<'tcx> {
21132113
) -> &'tcx rustc_hir::def_path_hash_map::DefPathHashMap {
21142114
// Create a dependency to the crate to be sure we re-execute this when the amount of
21152115
// definitions change.
2116-
self.ensure_ok().hir_crate(());
2116+
self.ensure_ok().hir_crate_items(());
21172117
// Freeze definitions once we start iterating on them, to prevent adding new ones
21182118
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
21192119
self.untracked.definitions.freeze().def_path_hash_to_def_index_map()
@@ -3160,42 +3160,33 @@ impl<'tcx> TyCtxt<'tcx> {
31603160
lint_level(self.sess, lint, level, Some(span.into()), decorate);
31613161
}
31623162

3163-
/// Find the crate root and the appropriate span where `use` and outer attributes can be
3164-
/// inserted at.
3165-
pub fn crate_level_attribute_injection_span(self, hir_id: HirId) -> Option<Span> {
3166-
for (_hir_id, node) in self.hir_parent_iter(hir_id) {
3167-
if let hir::Node::Crate(m) = node {
3168-
return Some(m.spans.inject_use_span.shrink_to_lo());
3169-
}
3170-
}
3171-
None
3163+
/// Find the appropriate span where `use` and outer attributes can be inserted at.
3164+
pub fn crate_level_attribute_injection_span(self) -> Span {
3165+
let node = self.hir_node(hir::CRATE_HIR_ID);
3166+
let hir::Node::Crate(m) = node else { bug!() };
3167+
m.spans.inject_use_span.shrink_to_lo()
31723168
}
31733169

31743170
pub fn disabled_nightly_features<E: rustc_errors::EmissionGuarantee>(
31753171
self,
31763172
diag: &mut Diag<'_, E>,
3177-
hir_id: Option<HirId>,
31783173
features: impl IntoIterator<Item = (String, Symbol)>,
31793174
) {
31803175
if !self.sess.is_nightly_build() {
31813176
return;
31823177
}
31833178

3184-
let span = hir_id.and_then(|id| self.crate_level_attribute_injection_span(id));
3179+
let span = self.crate_level_attribute_injection_span();
31853180
for (desc, feature) in features {
31863181
// FIXME: make this string translatable
31873182
let msg =
31883183
format!("add `#![feature({feature})]` to the crate attributes to enable{desc}");
3189-
if let Some(span) = span {
3190-
diag.span_suggestion_verbose(
3191-
span,
3192-
msg,
3193-
format!("#![feature({feature})]\n"),
3194-
Applicability::MaybeIncorrect,
3195-
);
3196-
} else {
3197-
diag.help(msg);
3198-
}
3184+
diag.span_suggestion_verbose(
3185+
span,
3186+
msg,
3187+
format!("#![feature({feature})]\n"),
3188+
Applicability::MaybeIncorrect,
3189+
);
31993190
}
32003191
}
32013192

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,11 +3575,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35753575
}
35763576
ObligationCauseCode::TrivialBound => {
35773577
err.help("see issue #48214");
3578-
tcx.disabled_nightly_features(
3579-
err,
3580-
Some(tcx.local_def_id_to_hir_id(body_id)),
3581-
[(String::new(), sym::trivial_bounds)],
3582-
);
3578+
tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
35833579
}
35843580
ObligationCauseCode::OpaqueReturnType(expr_info) => {
35853581
let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {

src/librustdoc/core.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ pub(crate) fn run_global_ctxt(
387387
ctxt.external_traits.insert(sized_trait_did, sized_trait);
388388
}
389389

390-
debug!("crate: {:?}", tcx.hir_crate(()));
391-
392390
let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt));
393391

394392
if krate.module.doc_value().is_empty() {

0 commit comments

Comments
 (0)