Skip to content

Commit 1033298

Browse files
Migrate resolver over to internal lint buffer
1 parent fa637fa commit 1033298

File tree

10 files changed

+70
-38
lines changed

10 files changed

+70
-38
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
@@ -1511,7 +1511,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
15111511
if is_expected(ctor_res) &&
15121512
self.r.is_accessible_from(ctor_vis, self.parent_scope.module) {
15131513
let lint = lint::builtin::LEGACY_CONSTRUCTOR_VISIBILITY;
1514-
self.r.session.buffer_lint(lint, id, span,
1514+
self.r.lint_buffer.buffer_lint(lint, id, span,
15151515
"private struct constructors are not usable through \
15161516
re-exports in outer modules",
15171517
);
@@ -1737,7 +1737,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
17371737
};
17381738
if result.base_res() == unqualified_result {
17391739
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
1740-
self.r.session.buffer_lint(lint, id, span, "unnecessary qualification")
1740+
self.r.lint_buffer.buffer_lint(lint, id, span, "unnecessary qualification")
17411741
}
17421742
}
17431743

@@ -2074,7 +2074,7 @@ impl<'a> Resolver<'a> {
20742074
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
20752075
visit::walk_crate(&mut late_resolution_visitor, krate);
20762076
for (id, span) in late_resolution_visitor.unused_labels.iter() {
2077-
self.session.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
2077+
self.lint_buffer.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
20782078
}
20792079
}
20802080
}

src/librustc_resolve/lib.rs

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

966968
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1087,7 +1089,8 @@ impl<'a> Resolver<'a> {
10871089
krate: &Crate,
10881090
crate_name: &str,
10891091
metadata_loader: &'a MetadataLoaderDyn,
1090-
arenas: &'a ResolverArenas<'a>)
1092+
arenas: &'a ResolverArenas<'a>,
1093+
lint_buffer: lint::LintBuffer)
10911094
-> Resolver<'a> {
10921095
let root_def_id = DefId::local(CRATE_DEF_INDEX);
10931096
let root_module_kind = ModuleKind::Def(
@@ -1226,7 +1229,8 @@ impl<'a> Resolver<'a> {
12261229
features.declared_lib_features.iter().map(|(feat, ..)| *feat)
12271230
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
12281231
.collect(),
1229-
variant_vis: Default::default()
1232+
variant_vis: Default::default(),
1233+
lint_buffer,
12301234
}
12311235
}
12321236

@@ -1652,7 +1656,7 @@ impl<'a> Resolver<'a> {
16521656
match result {
16531657
Ok(binding) => {
16541658
if let Some(node_id) = poisoned {
1655-
self.session.buffer_lint_with_diagnostic(
1659+
self.lint_buffer.buffer_lint_with_diagnostic(
16561660
lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
16571661
node_id, ident.span,
16581662
&format!("cannot find {} `{}` in this scope", ns.descr(), ident),
@@ -2117,7 +2121,7 @@ impl<'a> Resolver<'a> {
21172121
}
21182122

21192123
fn lint_if_path_starts_with_module(
2120-
&self,
2124+
&mut self,
21212125
crate_lint: CrateLint,
21222126
path: &[Segment],
21232127
path_span: Span,
@@ -2168,7 +2172,7 @@ impl<'a> Resolver<'a> {
21682172

21692173
let diag = lint::builtin::BuiltinLintDiagnostics
21702174
::AbsPathWithModule(diag_span);
2171-
self.session.buffer_lint_with_diagnostic(
2175+
self.lint_buffer.buffer_lint_with_diagnostic(
21722176
lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
21732177
diag_id, diag_span,
21742178
"absolute paths must start with `self`, `super`, \
@@ -2418,7 +2422,7 @@ impl<'a> Resolver<'a> {
24182422
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
24192423
let msg = "macro-expanded `macro_export` macros from the current crate \
24202424
cannot be referred to by absolute paths";
2421-
self.session.buffer_lint_with_diagnostic(
2425+
self.lint_buffer.buffer_lint_with_diagnostic(
24222426
lint::builtin::MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
24232427
CRATE_NODE_ID, span_use, msg,
24242428
lint::builtin::BuiltinLintDiagnostics::

src/librustc_resolve/macros.rs

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

249-
fn check_unused_macros(&self) {
249+
fn check_unused_macros(&mut self) {
250250
for (&node_id, &span) in self.unused_macros.iter() {
251-
self.session.buffer_lint(
251+
self.lint_buffer.buffer_lint(
252252
lint::builtin::UNUSED_MACROS, node_id, span, "unused macro definition"
253253
);
254254
}
@@ -788,15 +788,17 @@ impl<'a> Resolver<'a> {
788788
}
789789
}
790790

791-
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &ast::Path) {
791+
fn check_stability_and_deprecation(&mut self, ext: &SyntaxExtension, path: &ast::Path) {
792792
let span = path.span;
793793
if let Some(stability) = &ext.stability {
794794
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
795795
let feature = stability.feature;
796796
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
797797
let node_id = ast::CRATE_NODE_ID;
798-
let soft_handler =
799-
|lint, span, msg: &_| self.session.buffer_lint(lint, node_id, span, msg);
798+
let lint_buffer = &mut self.lint_buffer;
799+
let soft_handler = |lint, span, msg: &_| {
800+
lint_buffer.buffer_lint(lint, node_id, span, msg)
801+
};
800802
stability::report_unstable(
801803
self.session, feature, reason, issue, is_soft, span, soft_handler
802804
);
@@ -806,14 +808,14 @@ impl<'a> Resolver<'a> {
806808
let path = pprust::path_to_string(path);
807809
let (message, lint) = stability::rustc_deprecation_message(depr, &path);
808810
stability::early_report_deprecation(
809-
self.session, &message, depr.suggestion, lint, span
811+
&mut self.lint_buffer, &message, depr.suggestion, lint, span
810812
);
811813
}
812814
}
813815
if let Some(depr) = &ext.deprecation {
814816
let path = pprust::path_to_string(&path);
815817
let (message, lint) = stability::deprecation_message(depr, &path);
816-
stability::early_report_deprecation(self.session, &message, None, lint, span);
818+
stability::early_report_deprecation(&mut self.lint_buffer, &message, None, lint, span);
817819
}
818820
}
819821

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 3 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,
@@ -1147,7 +1147,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11471147
re-exported (error E0365), consider declaring with \
11481148
`pub`",
11491149
ident);
1150-
self.r.session.buffer_lint(PUB_USE_OF_PRIVATE_EXTERN_CRATE,
1150+
self.r.lint_buffer.buffer_lint(PUB_USE_OF_PRIVATE_EXTERN_CRATE,
11511151
directive.id,
11521152
directive.span,
11531153
&msg);
@@ -1272,7 +1272,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12721272
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
12731273
redundant_spans.sort();
12741274
redundant_spans.dedup();
1275-
self.r.session.buffer_lint_with_diagnostic(
1275+
self.r.lint_buffer.buffer_lint_with_diagnostic(
12761276
UNUSED_IMPORTS,
12771277
directive.id,
12781278
directive.span,

src/libsyntax_expand/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ pub trait Resolver {
868868
&mut self, invoc: &Invocation, eager_expansion_root: ExpnId, force: bool
869869
) -> Result<InvocationRes, Indeterminate>;
870870

871-
fn check_unused_macros(&self);
871+
fn check_unused_macros(&mut self);
872872

873873
fn has_derives(&self, expn_id: ExpnId, derives: SpecialDerives) -> bool;
874874
fn add_derives(&mut self, expn_id: ExpnId, derives: SpecialDerives);
@@ -1063,7 +1063,7 @@ impl<'a> ExtCtxt<'a> {
10631063
Symbol::intern(st)
10641064
}
10651065

1066-
pub fn check_unused_macros(&self) {
1066+
pub fn check_unused_macros(&mut self) {
10671067
self.resolver.check_unused_macros();
10681068
}
10691069

0 commit comments

Comments
 (0)