Rollup merge of #130419 - nnethercote:streamline-HirCollector, r=GuillaumeGomez

Streamline `HirCollector`

r? `@GuillaumeGomez`
This commit is contained in:
Matthias Krüger 2024-10-03 13:47:59 +02:00 committed by GitHub
commit c9b907a567
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 37 deletions

View File

@ -63,7 +63,7 @@ impl TraitOrTraitImpl {
} }
struct AstValidator<'a> { struct AstValidator<'a> {
session: &'a Session, sess: &'a Session,
features: &'a Features, features: &'a Features,
/// The span of the `extern` in an `extern { ... }` block, if any. /// The span of the `extern` in an `extern { ... }` block, if any.
@ -267,7 +267,7 @@ impl<'a> AstValidator<'a> {
} }
fn dcx(&self) -> DiagCtxtHandle<'a> { fn dcx(&self) -> DiagCtxtHandle<'a> {
self.session.dcx() self.sess.dcx()
} }
fn visibility_not_permitted(&self, vis: &Visibility, note: errors::VisibilityNotPermittedNote) { fn visibility_not_permitted(&self, vis: &Visibility, note: errors::VisibilityNotPermittedNote) {
@ -359,7 +359,7 @@ impl<'a> AstValidator<'a> {
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }), in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
const_context_label: parent_constness, const_context_label: parent_constness,
remove_const_sugg: ( remove_const_sugg: (
self.session.source_map().span_extend_while_whitespace(span), self.sess.source_map().span_extend_while_whitespace(span),
match parent_constness { match parent_constness {
Some(_) => rustc_errors::Applicability::MachineApplicable, Some(_) => rustc_errors::Applicability::MachineApplicable,
None => rustc_errors::Applicability::MaybeIncorrect, None => rustc_errors::Applicability::MaybeIncorrect,
@ -472,7 +472,7 @@ impl<'a> AstValidator<'a> {
fn check_defaultness(&self, span: Span, defaultness: Defaultness) { fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
if let Defaultness::Default(def_span) = defaultness { if let Defaultness::Default(def_span) = defaultness {
let span = self.session.source_map().guess_head_span(span); let span = self.sess.source_map().guess_head_span(span);
self.dcx().emit_err(errors::ForbiddenDefault { span, def_span }); self.dcx().emit_err(errors::ForbiddenDefault { span, def_span });
} }
} }
@ -480,7 +480,7 @@ impl<'a> AstValidator<'a> {
/// If `sp` ends with a semicolon, returns it as a `Span` /// If `sp` ends with a semicolon, returns it as a `Span`
/// Otherwise, returns `sp.shrink_to_hi()` /// Otherwise, returns `sp.shrink_to_hi()`
fn ending_semi_or_hi(&self, sp: Span) -> Span { fn ending_semi_or_hi(&self, sp: Span) -> Span {
let source_map = self.session.source_map(); let source_map = self.sess.source_map();
let end = source_map.end_point(sp); let end = source_map.end_point(sp);
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") { if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
@ -552,7 +552,7 @@ impl<'a> AstValidator<'a> {
} }
fn current_extern_span(&self) -> Span { fn current_extern_span(&self) -> Span {
self.session.source_map().guess_head_span(self.extern_mod.unwrap()) self.sess.source_map().guess_head_span(self.extern_mod.unwrap())
} }
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`. /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
@ -648,7 +648,7 @@ impl<'a> AstValidator<'a> {
if ident.name.as_str().is_ascii() { if ident.name.as_str().is_ascii() {
return; return;
} }
let span = self.session.source_map().guess_head_span(item_span); let span = self.sess.source_map().guess_head_span(item_span);
self.dcx().emit_err(errors::NoMangleAscii { span }); self.dcx().emit_err(errors::NoMangleAscii { span });
} }
@ -753,7 +753,7 @@ impl<'a> AstValidator<'a> {
self.dcx().emit_err(errors::PatternFnPointer { span }); self.dcx().emit_err(errors::PatternFnPointer { span });
}); });
if let Extern::Implicit(_) = bfty.ext { if let Extern::Implicit(_) = bfty.ext {
let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo()); let sig_span = self.sess.source_map().next_point(ty.span.shrink_to_lo());
self.maybe_lint_missing_abi(sig_span, ty.id); self.maybe_lint_missing_abi(sig_span, ty.id);
} }
} }
@ -795,7 +795,7 @@ impl<'a> AstValidator<'a> {
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the // FIXME(davidtwco): This is a hack to detect macros which produce spans of the
// call site which do not have a macro backtrace. See #61963. // call site which do not have a macro backtrace. See #61963.
if self if self
.session .sess
.source_map() .source_map()
.span_to_snippet(span) .span_to_snippet(span)
.is_ok_and(|snippet| !snippet.starts_with("#[")) .is_ok_and(|snippet| !snippet.starts_with("#["))
@ -885,7 +885,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
impl<'a> Visitor<'a> for AstValidator<'a> { impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_attribute(&mut self, attr: &Attribute) { fn visit_attribute(&mut self, attr: &Attribute) {
validate_attr::check_attr(&self.session.psess, attr); validate_attr::check_attr(&self.sess.psess, attr);
} }
fn visit_ty(&mut self, ty: &'a Ty) { fn visit_ty(&mut self, ty: &'a Ty) {
@ -1192,7 +1192,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} else if where_clauses.after.has_where_token { } else if where_clauses.after.has_where_token {
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias { self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
span: where_clauses.after.span, span: where_clauses.after.span,
help: self.session.is_nightly_build(), help: self.sess.is_nightly_build(),
}); });
} }
} }
@ -1328,7 +1328,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
(BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_)) (BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_))
if !self.features.more_maybe_bounds => if !self.features.more_maybe_bounds =>
{ {
self.session self.sess
.create_feature_err( .create_feature_err(
errors::OptionalTraitSupertrait { errors::OptionalTraitSupertrait {
span: trait_ref.span, span: trait_ref.span,
@ -1341,7 +1341,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
(BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_)) (BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_))
if !self.features.more_maybe_bounds => if !self.features.more_maybe_bounds =>
{ {
self.session self.sess
.create_feature_err( .create_feature_err(
errors::OptionalTraitObject { span: trait_ref.span }, errors::OptionalTraitObject { span: trait_ref.span },
sym::more_maybe_bounds, sym::more_maybe_bounds,
@ -1752,13 +1752,13 @@ fn deny_equality_constraints(
} }
pub fn check_crate( pub fn check_crate(
session: &Session, sess: &Session,
features: &Features, features: &Features,
krate: &Crate, krate: &Crate,
lints: &mut LintBuffer, lints: &mut LintBuffer,
) -> bool { ) -> bool {
let mut validator = AstValidator { let mut validator = AstValidator {
session, sess,
features, features,
extern_mod: None, extern_mod: None,
outer_trait_or_trait_impl: None, outer_trait_or_trait_impl: None,

View File

@ -186,8 +186,6 @@ pub(crate) fn run(
let mut collector = CreateRunnableDocTests::new(options, opts); let mut collector = CreateRunnableDocTests::new(options, opts);
let hir_collector = HirCollector::new( let hir_collector = HirCollector::new(
&compiler.sess,
tcx.hir(),
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()), ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
enable_per_target_ignores, enable_per_target_ignores,
tcx, tcx,

View File

@ -6,11 +6,9 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId}; use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
use rustc_hir::{self as hir, CRATE_HIR_ID, intravisit}; use rustc_hir::{self as hir, CRATE_HIR_ID, intravisit};
use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_resolve::rustdoc::span_of_fragments; use rustc_resolve::rustdoc::span_of_fragments;
use rustc_session::Session;
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, DUMMY_SP, FileName, Pos, Span}; use rustc_span::{BytePos, DUMMY_SP, FileName, Pos, Span};
@ -63,30 +61,22 @@ impl DocTestVisitor for RustCollector {
fn visit_header(&mut self, _name: &str, _level: u32) {} fn visit_header(&mut self, _name: &str, _level: u32) {}
} }
pub(super) struct HirCollector<'a, 'tcx> { pub(super) struct HirCollector<'tcx> {
sess: &'a Session,
map: Map<'tcx>,
codes: ErrorCodes, codes: ErrorCodes,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
enable_per_target_ignores: bool, enable_per_target_ignores: bool,
collector: RustCollector, collector: RustCollector,
} }
impl<'a, 'tcx> HirCollector<'a, 'tcx> { impl<'tcx> HirCollector<'tcx> {
pub fn new( pub fn new(codes: ErrorCodes, enable_per_target_ignores: bool, tcx: TyCtxt<'tcx>) -> Self {
sess: &'a Session,
map: Map<'tcx>,
codes: ErrorCodes,
enable_per_target_ignores: bool,
tcx: TyCtxt<'tcx>,
) -> Self {
let collector = RustCollector { let collector = RustCollector {
source_map: sess.psess.clone_source_map(), source_map: tcx.sess.psess.clone_source_map(),
cur_path: vec![], cur_path: vec![],
position: DUMMY_SP, position: DUMMY_SP,
tests: vec![], tests: vec![],
}; };
Self { sess, map, codes, enable_per_target_ignores, tcx, collector } Self { codes, enable_per_target_ignores, tcx, collector }
} }
pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> { pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> {
@ -98,7 +88,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
} }
} }
impl<'a, 'tcx> HirCollector<'a, 'tcx> { impl<'tcx> HirCollector<'tcx> {
fn visit_testable<F: FnOnce(&mut Self)>( fn visit_testable<F: FnOnce(&mut Self)>(
&mut self, &mut self,
name: String, name: String,
@ -108,7 +98,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
) { ) {
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id)); let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) { if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) { if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
return; return;
} }
} }
@ -141,17 +131,17 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
} }
} }
impl<'a, 'tcx> intravisit::Visitor<'tcx> for HirCollector<'a, 'tcx> { impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'tcx> {
type NestedFilter = nested_filter::All; type NestedFilter = nested_filter::All;
fn nested_visit_map(&mut self) -> Self::Map { fn nested_visit_map(&mut self) -> Self::Map {
self.map self.tcx.hir()
} }
fn visit_item(&mut self, item: &'tcx hir::Item<'_>) { fn visit_item(&mut self, item: &'tcx hir::Item<'_>) {
let name = match &item.kind { let name = match &item.kind {
hir::ItemKind::Impl(impl_) => { hir::ItemKind::Impl(impl_) => {
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id) rustc_hir_pretty::id_to_string(&self.tcx.hir(), impl_.self_ty.hir_id)
} }
_ => item.ident.to_string(), _ => item.ident.to_string(),
}; };