Skip to content

Commit bb0c930

Browse files
Migrate resolver over to internal lint buffer
1 parent 1bd6b48 commit bb0c930

File tree

10 files changed

+73
-39
lines changed

10 files changed

+73
-39
lines changed

src/librustc/lint/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,27 @@ impl LintBuffer {
646646
fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
647647
self.map.remove(&id).unwrap_or_default()
648648
}
649+
650+
pub fn buffer_lint<S: Into<MultiSpan>>(
651+
&mut self,
652+
lint: &'static Lint,
653+
id: ast::NodeId,
654+
sp: S,
655+
msg: &str,
656+
) {
657+
self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
658+
}
659+
660+
pub fn buffer_lint_with_diagnostic<S: Into<MultiSpan>>(
661+
&mut self,
662+
lint: &'static Lint,
663+
id: ast::NodeId,
664+
sp: S,
665+
msg: &str,
666+
diagnostic: BuiltinLintDiagnostics,
667+
) {
668+
self.add_lint(lint, id, sp.into(), msg, diagnostic)
669+
}
649670
}
650671

651672
pub fn struct_lint_level<'a>(sess: &'a Session,

src/librustc/middle/stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ pub fn rustc_deprecation_message(depr: &RustcDeprecation, path: &str) -> (String
586586
}
587587

588588
pub fn early_report_deprecation(
589-
sess: &Session,
589+
lint_buffer: &'a mut lint::LintBuffer,
590590
message: &str,
591591
suggestion: Option<Symbol>,
592592
lint: &'static Lint,
@@ -597,7 +597,7 @@ pub fn early_report_deprecation(
597597
}
598598

599599
let diag = BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span);
600-
sess.buffer_lint_with_diagnostic(lint, CRATE_NODE_ID, span, message, diag);
600+
lint_buffer.buffer_lint_with_diagnostic(lint, CRATE_NODE_ID, span, message, diag);
601601
}
602602

603603
fn late_report_deprecation(

src/librustc_interface/passes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,14 @@ fn configure_and_expand_inner<'a>(
270270
rustc_lint::BuiltinCombinedPreExpansionLintPass::new());
271271
});
272272

273+
let lint_buffer = lint::LintBuffer::default();
273274
let mut resolver = Resolver::new(
274275
sess,
275276
&krate,
276277
crate_name,
277278
metadata_loader,
278279
&resolver_arenas,
280+
lint_buffer,
279281
);
280282
syntax_ext::register_builtin_macros(&mut resolver, sess.edition());
281283

@@ -366,7 +368,7 @@ fn configure_and_expand_inner<'a>(
366368
for span in missing_fragment_specifiers {
367369
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
368370
let msg = "missing fragment specifier";
369-
sess.buffer_lint(lint, ast::CRATE_NODE_ID, span, msg);
371+
resolver.lint_buffer.buffer_lint(lint, ast::CRATE_NODE_ID, span, msg);
370372
}
371373
if cfg!(windows) {
372374
env::set_var("PATH", &old_path);
@@ -395,7 +397,7 @@ fn configure_and_expand_inner<'a>(
395397
}
396398

397399
let has_proc_macro_decls = time(sess, "AST validation", || {
398-
ast_validation::check_crate(sess, &krate)
400+
ast_validation::check_crate(sess, &krate, &mut resolver.lint_buffer)
399401
});
400402

401403

@@ -464,7 +466,7 @@ fn configure_and_expand_inner<'a>(
464466
info!("{} parse sess buffered_lints", buffered_lints.len());
465467
for BufferedEarlyLint{id, span, msg, lint_id} in buffered_lints.drain(..) {
466468
let lint = lint::Lint::from_parser_lint_id(lint_id);
467-
sess.buffer_lint(lint, id, span, &msg);
469+
resolver.lint_buffer.buffer_lint(lint, id, span, &msg);
468470
}
469471
});
470472

src/librustc_passes/ast_validation.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ struct AstValidator<'a> {
7373
/// these booleans.
7474
warning_period_57979_didnt_record_next_impl_trait: bool,
7575
warning_period_57979_impl_trait_in_proj: bool,
76+
77+
lint_buffer: &'a mut lint::LintBuffer,
7678
}
7779

7880
impl<'a> AstValidator<'a> {
@@ -229,7 +231,7 @@ impl<'a> AstValidator<'a> {
229231
err.emit();
230232
}
231233

232-
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
234+
fn check_decl_no_pat<F: FnMut(Span, bool)>(decl: &FnDecl, mut report_err: F) {
233235
for arg in &decl.inputs {
234236
match arg.pat.kind {
235237
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
@@ -460,7 +462,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
460462
match ty.kind {
461463
TyKind::BareFn(ref bfty) => {
462464
self.check_fn_decl(&bfty.decl);
463-
self.check_decl_no_pat(&bfty.decl, |span, _| {
465+
Self::check_decl_no_pat(&bfty.decl, |span, _| {
464466
struct_span_err!(self.session, span, E0561,
465467
"patterns aren't allowed in function pointer types").emit();
466468
});
@@ -483,7 +485,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
483485
TyKind::ImplTrait(_, ref bounds) => {
484486
if self.is_impl_trait_banned {
485487
if self.warning_period_57979_impl_trait_in_proj {
486-
self.session.buffer_lint(
488+
self.lint_buffer.buffer_lint(
487489
NESTED_IMPL_TRAIT, ty.id, ty.span,
488490
"`impl Trait` is not allowed in path parameters");
489491
} else {
@@ -494,7 +496,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
494496

495497
if let Some(outer_impl_trait) = self.outer_impl_trait {
496498
if outer_impl_trait.should_warn_instead_of_error() {
497-
self.session.buffer_lint_with_diagnostic(
499+
self.lint_buffer.buffer_lint_with_diagnostic(
498500
NESTED_IMPL_TRAIT, ty.id, ty.span,
499501
"nested `impl Trait` is not allowed",
500502
BuiltinLintDiagnostics::NestedImplTrait {
@@ -634,9 +636,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
634636
self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness.node);
635637
self.check_trait_fn_not_const(sig.header.constness);
636638
if block.is_none() {
637-
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
639+
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
638640
if mut_ident {
639-
self.session.buffer_lint(
641+
self.lint_buffer.buffer_lint(
640642
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
641643
trait_item.id, span,
642644
"patterns aren't allowed in methods without bodies");
@@ -655,7 +657,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
655657
if attr::contains_name(&item.attrs, sym::warn_directory_ownership) {
656658
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
657659
let msg = "cannot declare a new module at this location";
658-
self.session.buffer_lint(lint, item.id, item.span, msg);
660+
self.lint_buffer.buffer_lint(lint, item.id, item.span, msg);
659661
}
660662
}
661663
ItemKind::Union(ref vdata, _) => {
@@ -686,7 +688,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
686688
match fi.kind {
687689
ForeignItemKind::Fn(ref decl, _) => {
688690
self.check_fn_decl(decl);
689-
self.check_decl_no_pat(decl, |span, _| {
691+
Self::check_decl_no_pat(decl, |span, _| {
690692
struct_span_err!(self.session, span, E0130,
691693
"patterns aren't allowed in foreign function declarations")
692694
.span_label(span, "pattern not allowed in foreign function").emit();
@@ -840,7 +842,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
840842
}
841843
}
842844

843-
pub fn check_crate(session: &Session, krate: &Crate) -> bool {
845+
pub fn check_crate(session: &Session, krate: &Crate, lints: &mut lint::LintBuffer) -> bool {
844846
let mut validator = AstValidator {
845847
session,
846848
has_proc_macro_decls: false,
@@ -849,6 +851,7 @@ pub fn check_crate(session: &Session, krate: &Crate) -> bool {
849851
is_assoc_ty_bound_banned: false,
850852
warning_period_57979_didnt_record_next_impl_trait: false,
851853
warning_period_57979_impl_trait_in_proj: false,
854+
lint_buffer: lints,
852855
};
853856
visit::walk_crate(&mut validator, krate);
854857

src/librustc_resolve/check_unused.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl Resolver<'_> {
232232
directive.span.is_dummy() => {
233233
if let ImportDirectiveSubclass::MacroUse = directive.subclass {
234234
if !directive.span.is_dummy() {
235-
self.session.buffer_lint(
235+
self.lint_buffer.buffer_lint(
236236
lint::builtin::MACRO_USE_EXTERN_CRATE,
237237
directive.id,
238238
directive.span,
@@ -250,7 +250,7 @@ impl Resolver<'_> {
250250
ImportDirectiveSubclass::MacroUse => {
251251
let lint = lint::builtin::UNUSED_IMPORTS;
252252
let msg = "unused `#[macro_use]` import";
253-
self.session.buffer_lint(lint, directive.id, directive.span, msg);
253+
self.lint_buffer.buffer_lint(lint, directive.id, directive.span, msg);
254254
}
255255
_ => {}
256256
}
@@ -312,7 +312,7 @@ impl Resolver<'_> {
312312
"remove the unused import"
313313
};
314314

315-
visitor.r.session.buffer_lint_with_diagnostic(
315+
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
316316
lint::builtin::UNUSED_IMPORTS,
317317
unused.use_tree_id,
318318
ms,

src/librustc_resolve/late.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
15481548
if is_expected(ctor_res) &&
15491549
self.r.is_accessible_from(ctor_vis, self.parent_scope.module) {
15501550
let lint = lint::builtin::LEGACY_CONSTRUCTOR_VISIBILITY;
1551-
self.r.session.buffer_lint(lint, id, span,
1551+
self.r.lint_buffer.buffer_lint(lint, id, span,
15521552
"private struct constructors are not usable through \
15531553
re-exports in outer modules",
15541554
);
@@ -1774,7 +1774,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
17741774
};
17751775
if result.base_res() == unqualified_result {
17761776
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
1777-
self.r.session.buffer_lint(lint, id, span, "unnecessary qualification")
1777+
self.r.lint_buffer.buffer_lint(lint, id, span, "unnecessary qualification")
17781778
}
17791779
}
17801780

@@ -2111,7 +2111,7 @@ impl<'a> Resolver<'a> {
21112111
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
21122112
visit::walk_crate(&mut late_resolution_visitor, krate);
21132113
for (id, span) in late_resolution_visitor.diagnostic_metadata.unused_labels.iter() {
2114-
self.session.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
2114+
self.lint_buffer.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
21152115
}
21162116
}
21172117
}

src/librustc_resolve/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ pub struct Resolver<'a> {
962962
/// Stores enum visibilities to properly build a reduced graph
963963
/// when visiting the correspondent variants.
964964
variant_vis: DefIdMap<ty::Visibility>,
965+
966+
pub lint_buffer: lint::LintBuffer,
965967
}
966968

967969
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1088,7 +1090,8 @@ impl<'a> Resolver<'a> {
10881090
krate: &Crate,
10891091
crate_name: &str,
10901092
metadata_loader: &'a MetadataLoaderDyn,
1091-
arenas: &'a ResolverArenas<'a>)
1093+
arenas: &'a ResolverArenas<'a>,
1094+
lint_buffer: lint::LintBuffer)
10921095
-> Resolver<'a> {
10931096
let root_def_id = DefId::local(CRATE_DEF_INDEX);
10941097
let root_module_kind = ModuleKind::Def(
@@ -1227,7 +1230,8 @@ impl<'a> Resolver<'a> {
12271230
features.declared_lib_features.iter().map(|(feat, ..)| *feat)
12281231
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
12291232
.collect(),
1230-
variant_vis: Default::default()
1233+
variant_vis: Default::default(),
1234+
lint_buffer,
12311235
}
12321236
}
12331237

@@ -1653,7 +1657,7 @@ impl<'a> Resolver<'a> {
16531657
match result {
16541658
Ok(binding) => {
16551659
if let Some(node_id) = poisoned {
1656-
self.session.buffer_lint_with_diagnostic(
1660+
self.lint_buffer.buffer_lint_with_diagnostic(
16571661
lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
16581662
node_id, ident.span,
16591663
&format!("cannot find {} `{}` in this scope", ns.descr(), ident),
@@ -2118,7 +2122,7 @@ impl<'a> Resolver<'a> {
21182122
}
21192123

21202124
fn lint_if_path_starts_with_module(
2121-
&self,
2125+
&mut self,
21222126
crate_lint: CrateLint,
21232127
path: &[Segment],
21242128
path_span: Span,
@@ -2169,7 +2173,7 @@ impl<'a> Resolver<'a> {
21692173

21702174
let diag = lint::builtin::BuiltinLintDiagnostics
21712175
::AbsPathWithModule(diag_span);
2172-
self.session.buffer_lint_with_diagnostic(
2176+
self.lint_buffer.buffer_lint_with_diagnostic(
21732177
lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
21742178
diag_id, diag_span,
21752179
"absolute paths must start with `self`, `super`, \
@@ -2419,7 +2423,7 @@ impl<'a> Resolver<'a> {
24192423
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
24202424
let msg = "macro-expanded `macro_export` macros from the current crate \
24212425
cannot be referred to by absolute paths";
2422-
self.session.buffer_lint_with_diagnostic(
2426+
self.lint_buffer.buffer_lint_with_diagnostic(
24232427
lint::builtin::MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
24242428
CRATE_NODE_ID, span_use, msg,
24252429
lint::builtin::BuiltinLintDiagnostics::

src/librustc_resolve/macros.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ impl<'a> base::Resolver for Resolver<'a> {
247247
Ok(InvocationRes::Single(ext))
248248
}
249249

250-
fn check_unused_macros(&self) {
250+
fn check_unused_macros(&mut self) {
251251
for (&node_id, &span) in self.unused_macros.iter() {
252-
self.session.buffer_lint(
252+
self.lint_buffer.buffer_lint(
253253
lint::builtin::UNUSED_MACROS, node_id, span, "unused macro definition"
254254
);
255255
}
@@ -789,15 +789,17 @@ impl<'a> Resolver<'a> {
789789
}
790790
}
791791

792-
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &ast::Path) {
792+
fn check_stability_and_deprecation(&mut self, ext: &SyntaxExtension, path: &ast::Path) {
793793
let span = path.span;
794794
if let Some(stability) = &ext.stability {
795795
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
796796
let feature = stability.feature;
797797
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
798798
let node_id = ast::CRATE_NODE_ID;
799-
let soft_handler =
800-
|lint, span, msg: &_| self.session.buffer_lint(lint, node_id, span, msg);
799+
let lint_buffer = &mut self.lint_buffer;
800+
let soft_handler = |lint, span, msg: &_| {
801+
lint_buffer.buffer_lint(lint, node_id, span, msg)
802+
};
801803
stability::report_unstable(
802804
self.session, feature, reason, issue, is_soft, span, soft_handler
803805
);
@@ -807,14 +809,14 @@ impl<'a> Resolver<'a> {
807809
let path = pprust::path_to_string(path);
808810
let (message, lint) = stability::rustc_deprecation_message(depr, &path);
809811
stability::early_report_deprecation(
810-
self.session, &message, depr.suggestion, lint, span
812+
&mut self.lint_buffer, &message, depr.suggestion, lint, span
811813
);
812814
}
813815
}
814816
if let Some(depr) = &ext.deprecation {
815817
let path = pprust::path_to_string(&path);
816818
let (message, lint) = stability::deprecation_message(depr, &path);
817-
stability::early_report_deprecation(self.session, &message, None, lint, span);
819+
stability::early_report_deprecation(&mut self.lint_buffer, &message, None, lint, span);
818820
}
819821
}
820822

src/librustc_resolve/resolve_imports.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'a> Resolver<'a> {
496496
if let (&NameBindingKind::Res(_, true), &NameBindingKind::Res(_, true)) =
497497
(&old_binding.kind, &binding.kind) {
498498

499-
this.session.buffer_lint_with_diagnostic(
499+
this.lint_buffer.buffer_lint_with_diagnostic(
500500
DUPLICATE_MACRO_EXPORTS,
501501
CRATE_NODE_ID,
502502
binding.span,
@@ -979,7 +979,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
979979
!max_vis.get().is_at_least(directive.vis.get(), &*self) {
980980
let msg =
981981
"glob import doesn't reexport anything because no candidate is public enough";
982-
self.r.session.buffer_lint(UNUSED_IMPORTS, directive.id, directive.span, msg);
982+
self.r.lint_buffer.buffer_lint(
983+
UNUSED_IMPORTS, directive.id, directive.span, msg,
984+
);
983985
}
984986
return None;
985987
}
@@ -1148,7 +1150,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11481150
re-exported (error E0365), consider declaring with \
11491151
`pub`",
11501152
ident);
1151-
self.r.session.buffer_lint(PUB_USE_OF_PRIVATE_EXTERN_CRATE,
1153+
self.r.lint_buffer.buffer_lint(PUB_USE_OF_PRIVATE_EXTERN_CRATE,
11521154
directive.id,
11531155
directive.span,
11541156
&msg);
@@ -1273,7 +1275,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12731275
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
12741276
redundant_spans.sort();
12751277
redundant_spans.dedup();
1276-
self.r.session.buffer_lint_with_diagnostic(
1278+
self.r.lint_buffer.buffer_lint_with_diagnostic(
12771279
UNUSED_IMPORTS,
12781280
directive.id,
12791281
directive.span,

0 commit comments

Comments
 (0)