Skip to content

Commit c9b907a

Browse files
authored
Rollup merge of #130419 - nnethercote:streamline-HirCollector, r=GuillaumeGomez
Streamline `HirCollector` r? `@GuillaumeGomez`
2 parents f7c8928 + 0882ad5 commit c9b907a

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl TraitOrTraitImpl {
6363
}
6464

6565
struct AstValidator<'a> {
66-
session: &'a Session,
66+
sess: &'a Session,
6767
features: &'a Features,
6868

6969
/// The span of the `extern` in an `extern { ... }` block, if any.
@@ -267,7 +267,7 @@ impl<'a> AstValidator<'a> {
267267
}
268268

269269
fn dcx(&self) -> DiagCtxtHandle<'a> {
270-
self.session.dcx()
270+
self.sess.dcx()
271271
}
272272

273273
fn visibility_not_permitted(&self, vis: &Visibility, note: errors::VisibilityNotPermittedNote) {
@@ -359,7 +359,7 @@ impl<'a> AstValidator<'a> {
359359
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
360360
const_context_label: parent_constness,
361361
remove_const_sugg: (
362-
self.session.source_map().span_extend_while_whitespace(span),
362+
self.sess.source_map().span_extend_while_whitespace(span),
363363
match parent_constness {
364364
Some(_) => rustc_errors::Applicability::MachineApplicable,
365365
None => rustc_errors::Applicability::MaybeIncorrect,
@@ -472,15 +472,15 @@ impl<'a> AstValidator<'a> {
472472

473473
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
474474
if let Defaultness::Default(def_span) = defaultness {
475-
let span = self.session.source_map().guess_head_span(span);
475+
let span = self.sess.source_map().guess_head_span(span);
476476
self.dcx().emit_err(errors::ForbiddenDefault { span, def_span });
477477
}
478478
}
479479

480480
/// If `sp` ends with a semicolon, returns it as a `Span`
481481
/// Otherwise, returns `sp.shrink_to_hi()`
482482
fn ending_semi_or_hi(&self, sp: Span) -> Span {
483-
let source_map = self.session.source_map();
483+
let source_map = self.sess.source_map();
484484
let end = source_map.end_point(sp);
485485

486486
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
@@ -552,7 +552,7 @@ impl<'a> AstValidator<'a> {
552552
}
553553

554554
fn current_extern_span(&self) -> Span {
555-
self.session.source_map().guess_head_span(self.extern_mod.unwrap())
555+
self.sess.source_map().guess_head_span(self.extern_mod.unwrap())
556556
}
557557

558558
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
@@ -648,7 +648,7 @@ impl<'a> AstValidator<'a> {
648648
if ident.name.as_str().is_ascii() {
649649
return;
650650
}
651-
let span = self.session.source_map().guess_head_span(item_span);
651+
let span = self.sess.source_map().guess_head_span(item_span);
652652
self.dcx().emit_err(errors::NoMangleAscii { span });
653653
}
654654

@@ -753,7 +753,7 @@ impl<'a> AstValidator<'a> {
753753
self.dcx().emit_err(errors::PatternFnPointer { span });
754754
});
755755
if let Extern::Implicit(_) = bfty.ext {
756-
let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo());
756+
let sig_span = self.sess.source_map().next_point(ty.span.shrink_to_lo());
757757
self.maybe_lint_missing_abi(sig_span, ty.id);
758758
}
759759
}
@@ -795,7 +795,7 @@ impl<'a> AstValidator<'a> {
795795
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
796796
// call site which do not have a macro backtrace. See #61963.
797797
if self
798-
.session
798+
.sess
799799
.source_map()
800800
.span_to_snippet(span)
801801
.is_ok_and(|snippet| !snippet.starts_with("#["))
@@ -885,7 +885,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
885885

886886
impl<'a> Visitor<'a> for AstValidator<'a> {
887887
fn visit_attribute(&mut self, attr: &Attribute) {
888-
validate_attr::check_attr(&self.session.psess, attr);
888+
validate_attr::check_attr(&self.sess.psess, attr);
889889
}
890890

891891
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -1192,7 +1192,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11921192
} else if where_clauses.after.has_where_token {
11931193
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
11941194
span: where_clauses.after.span,
1195-
help: self.session.is_nightly_build(),
1195+
help: self.sess.is_nightly_build(),
11961196
});
11971197
}
11981198
}
@@ -1328,7 +1328,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13281328
(BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_))
13291329
if !self.features.more_maybe_bounds =>
13301330
{
1331-
self.session
1331+
self.sess
13321332
.create_feature_err(
13331333
errors::OptionalTraitSupertrait {
13341334
span: trait_ref.span,
@@ -1341,7 +1341,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13411341
(BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_))
13421342
if !self.features.more_maybe_bounds =>
13431343
{
1344-
self.session
1344+
self.sess
13451345
.create_feature_err(
13461346
errors::OptionalTraitObject { span: trait_ref.span },
13471347
sym::more_maybe_bounds,
@@ -1752,13 +1752,13 @@ fn deny_equality_constraints(
17521752
}
17531753

17541754
pub fn check_crate(
1755-
session: &Session,
1755+
sess: &Session,
17561756
features: &Features,
17571757
krate: &Crate,
17581758
lints: &mut LintBuffer,
17591759
) -> bool {
17601760
let mut validator = AstValidator {
1761-
session,
1761+
sess,
17621762
features,
17631763
extern_mod: None,
17641764
outer_trait_or_trait_impl: None,

src/librustdoc/doctest.rs

-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ pub(crate) fn run(
186186

187187
let mut collector = CreateRunnableDocTests::new(options, opts);
188188
let hir_collector = HirCollector::new(
189-
&compiler.sess,
190-
tcx.hir(),
191189
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
192190
enable_per_target_ignores,
193191
tcx,

src/librustdoc/doctest/rust.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ use rustc_data_structures::fx::FxHashSet;
66
use rustc_data_structures::sync::Lrc;
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
88
use rustc_hir::{self as hir, CRATE_HIR_ID, intravisit};
9-
use rustc_middle::hir::map::Map;
109
use rustc_middle::hir::nested_filter;
1110
use rustc_middle::ty::TyCtxt;
1211
use rustc_resolve::rustdoc::span_of_fragments;
13-
use rustc_session::Session;
1412
use rustc_span::source_map::SourceMap;
1513
use rustc_span::{BytePos, DUMMY_SP, FileName, Pos, Span};
1614

@@ -63,30 +61,22 @@ impl DocTestVisitor for RustCollector {
6361
fn visit_header(&mut self, _name: &str, _level: u32) {}
6462
}
6563

66-
pub(super) struct HirCollector<'a, 'tcx> {
67-
sess: &'a Session,
68-
map: Map<'tcx>,
64+
pub(super) struct HirCollector<'tcx> {
6965
codes: ErrorCodes,
7066
tcx: TyCtxt<'tcx>,
7167
enable_per_target_ignores: bool,
7268
collector: RustCollector,
7369
}
7470

75-
impl<'a, 'tcx> HirCollector<'a, 'tcx> {
76-
pub fn new(
77-
sess: &'a Session,
78-
map: Map<'tcx>,
79-
codes: ErrorCodes,
80-
enable_per_target_ignores: bool,
81-
tcx: TyCtxt<'tcx>,
82-
) -> Self {
71+
impl<'tcx> HirCollector<'tcx> {
72+
pub fn new(codes: ErrorCodes, enable_per_target_ignores: bool, tcx: TyCtxt<'tcx>) -> Self {
8373
let collector = RustCollector {
84-
source_map: sess.psess.clone_source_map(),
74+
source_map: tcx.sess.psess.clone_source_map(),
8575
cur_path: vec![],
8676
position: DUMMY_SP,
8777
tests: vec![],
8878
};
89-
Self { sess, map, codes, enable_per_target_ignores, tcx, collector }
79+
Self { codes, enable_per_target_ignores, tcx, collector }
9080
}
9181

9282
pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> {
@@ -98,7 +88,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
9888
}
9989
}
10090

101-
impl<'a, 'tcx> HirCollector<'a, 'tcx> {
91+
impl<'tcx> HirCollector<'tcx> {
10292
fn visit_testable<F: FnOnce(&mut Self)>(
10393
&mut self,
10494
name: String,
@@ -108,7 +98,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
10898
) {
10999
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
110100
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
111-
if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) {
101+
if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
112102
return;
113103
}
114104
}
@@ -141,17 +131,17 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
141131
}
142132
}
143133

144-
impl<'a, 'tcx> intravisit::Visitor<'tcx> for HirCollector<'a, 'tcx> {
134+
impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'tcx> {
145135
type NestedFilter = nested_filter::All;
146136

147137
fn nested_visit_map(&mut self) -> Self::Map {
148-
self.map
138+
self.tcx.hir()
149139
}
150140

151141
fn visit_item(&mut self, item: &'tcx hir::Item<'_>) {
152142
let name = match &item.kind {
153143
hir::ItemKind::Impl(impl_) => {
154-
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
144+
rustc_hir_pretty::id_to_string(&self.tcx.hir(), impl_.self_ty.hir_id)
155145
}
156146
_ => item.ident.to_string(),
157147
};

0 commit comments

Comments
 (0)