Skip to content

Commit 452aa81

Browse files
committed
rustc_lint: Remove some redundant fields from EarlyContext
Use consistent function parameter order for early context construction and early linting Rename some functions to make it clear that they do not necessarily work on the whole crate
1 parent d13e8dd commit 452aa81

File tree

8 files changed

+70
-94
lines changed

8 files changed

+70
-94
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,19 @@ pub fn register_plugins<'a>(
236236
fn pre_expansion_lint(
237237
sess: &Session,
238238
lint_store: &LintStore,
239-
krate: &ast::Crate,
240239
crate_attrs: &[ast::Attribute],
241-
crate_name: &str,
240+
check_node: &ast::Crate,
241+
node_name: &str,
242242
) {
243-
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", crate_name).run(|| {
244-
rustc_lint::check_ast_crate(
243+
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name).run(|| {
244+
rustc_lint::check_ast_node(
245245
sess,
246+
true,
246247
lint_store,
247-
krate,
248248
crate_attrs,
249-
true,
250249
None,
251250
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
251+
check_node,
252252
);
253253
});
254254
}
@@ -265,7 +265,7 @@ pub fn configure_and_expand(
265265
resolver: &mut Resolver<'_>,
266266
) -> Result<ast::Crate> {
267267
tracing::trace!("configure_and_expand");
268-
pre_expansion_lint(sess, lint_store, &krate, &krate.attrs, crate_name);
268+
pre_expansion_lint(sess, lint_store, &krate.attrs, &krate, crate_name);
269269
rustc_builtin_macros::register_builtin_macros(resolver);
270270

271271
krate = sess.time("crate_injection", || {
@@ -324,7 +324,7 @@ pub fn configure_and_expand(
324324
let crate_attrs = krate.attrs.clone();
325325
let extern_mod_loaded = |ident: Ident, attrs, items, span| {
326326
let krate = ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false };
327-
pre_expansion_lint(sess, lint_store, &krate, &crate_attrs, ident.name.as_str());
327+
pre_expansion_lint(sess, lint_store, &crate_attrs, &krate, ident.name.as_str());
328328
(krate.attrs, krate.items)
329329
};
330330
let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&extern_mod_loaded));
@@ -499,14 +499,14 @@ pub fn lower_to_hir<'res, 'tcx>(
499499
);
500500

501501
sess.time("early_lint_checks", || {
502-
rustc_lint::check_ast_crate(
502+
rustc_lint::check_ast_node(
503503
sess,
504+
false,
504505
lint_store,
505-
&krate,
506506
&krate.attrs,
507-
false,
508507
Some(std::mem::take(resolver.lint_buffer())),
509508
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
509+
&krate,
510510
)
511511
});
512512

compiler/rustc_lint/src/builtin.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ declare_lint_pass!(
912912

913913
impl EarlyLintPass for AnonymousParameters {
914914
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
915-
if cx.sess.edition() != Edition::Edition2015 {
915+
if cx.sess().edition() != Edition::Edition2015 {
916916
// This is a hard error in future editions; avoid linting and erroring
917917
return;
918918
}
@@ -921,7 +921,7 @@ impl EarlyLintPass for AnonymousParameters {
921921
if let ast::PatKind::Ident(_, ident, None) = arg.pat.kind {
922922
if ident.name == kw::Empty {
923923
cx.struct_span_lint(ANONYMOUS_PARAMETERS, arg.pat.span, |lint| {
924-
let ty_snip = cx.sess.source_map().span_to_snippet(arg.ty.span);
924+
let ty_snip = cx.sess().source_map().span_to_snippet(arg.ty.span);
925925

926926
let (ty_snip, appl) = if let Ok(ref snip) = ty_snip {
927927
(snip.as_str(), Applicability::MachineApplicable)
@@ -1775,7 +1775,7 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17751775
};
17761776
if join.edition() >= Edition::Edition2021 {
17771777
let mut err =
1778-
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1778+
rustc_errors::struct_span_err!(cx.sess(), pat.span, E0783, "{}", msg,);
17791779
err.span_suggestion(
17801780
pat.span,
17811781
suggestion,
@@ -1799,7 +1799,7 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17991799
let replace = "..=".to_owned();
18001800
if join.edition() >= Edition::Edition2021 {
18011801
let mut err =
1802-
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1802+
rustc_errors::struct_span_err!(cx.sess(), pat.span, E0783, "{}", msg,);
18031803
err.span_suggestion_short(
18041804
join,
18051805
suggestion,
@@ -1983,7 +1983,7 @@ impl KeywordIdents {
19831983
UnderMacro(under_macro): UnderMacro,
19841984
ident: Ident,
19851985
) {
1986-
let next_edition = match cx.sess.edition() {
1986+
let next_edition = match cx.sess().edition() {
19871987
Edition::Edition2015 => {
19881988
match ident.name {
19891989
kw::Async | kw::Await | kw::Try => Edition::Edition2018,
@@ -2011,7 +2011,7 @@ impl KeywordIdents {
20112011
};
20122012

20132013
// Don't lint `r#foo`.
2014-
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&ident.span) {
2014+
if cx.sess().parse_sess.raw_identifier_spans.borrow().contains(&ident.span) {
20152015
return;
20162016
}
20172017

@@ -2379,7 +2379,7 @@ declare_lint_pass!(
23792379

23802380
impl EarlyLintPass for IncompleteFeatures {
23812381
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
2382-
let features = cx.sess.features_untracked();
2382+
let features = cx.sess().features_untracked();
23832383
features
23842384
.declared_lang_features
23852385
.iter()

compiler/rustc_lint/src/context.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -553,20 +553,9 @@ pub struct LateContext<'tcx> {
553553
pub only_module: bool,
554554
}
555555

556-
/// Context for lint checking of the AST, after expansion, before lowering to
557-
/// HIR.
556+
/// Context for lint checking of the AST, after expansion, before lowering to HIR.
558557
pub struct EarlyContext<'a> {
559-
/// Type context we're checking in.
560-
pub sess: &'a Session,
561-
562-
/// The crate being checked.
563-
pub krate: &'a ast::Crate,
564-
565558
pub builder: LintLevelsBuilder<'a>,
566-
567-
/// The store of registered lints and the lint levels.
568-
pub lint_store: &'a LintStore,
569-
570559
pub buffered: LintBuffer,
571560
}
572561

@@ -801,18 +790,14 @@ pub trait LintContext: Sized {
801790
}
802791

803792
impl<'a> EarlyContext<'a> {
804-
pub fn new(
793+
pub(crate) fn new(
805794
sess: &'a Session,
795+
warn_about_weird_lints: bool,
806796
lint_store: &'a LintStore,
807-
krate: &'a ast::Crate,
808797
crate_attrs: &'a [ast::Attribute],
809798
buffered: LintBuffer,
810-
warn_about_weird_lints: bool,
811799
) -> EarlyContext<'a> {
812800
EarlyContext {
813-
sess,
814-
krate,
815-
lint_store,
816801
builder: LintLevelsBuilder::new(sess, warn_about_weird_lints, lint_store, crate_attrs),
817802
buffered,
818803
}
@@ -851,11 +836,11 @@ impl LintContext for EarlyContext<'_> {
851836

852837
/// Gets the overall compiler `Session` object.
853838
fn sess(&self) -> &Session {
854-
&self.sess
839+
&self.builder.sess()
855840
}
856841

857842
fn lints(&self) -> &LintStore {
858-
&*self.lint_store
843+
self.builder.lint_store()
859844
}
860845

861846
fn lookup<S: Into<MultiSpan>>(

compiler/rustc_lint/src/early.rs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
5757
F: FnOnce(&mut Self),
5858
{
5959
let is_crate_node = id == ast::CRATE_NODE_ID;
60-
let push = self.context.builder.push(attrs, &self.context.lint_store, is_crate_node);
60+
let push = self.context.builder.push(attrs, is_crate_node);
6161
self.check_id(id);
6262
self.enter_attrs(attrs);
6363
f(self);
@@ -325,88 +325,76 @@ macro_rules! early_lint_pass_impl {
325325

326326
crate::early_lint_methods!(early_lint_pass_impl, []);
327327

328-
fn early_lint_crate<T: EarlyLintPass>(
328+
fn early_lint_node(
329329
sess: &Session,
330+
warn_about_weird_lints: bool,
330331
lint_store: &LintStore,
331-
krate: &ast::Crate,
332332
crate_attrs: &[ast::Attribute],
333-
pass: T,
334333
buffered: LintBuffer,
335-
warn_about_weird_lints: bool,
334+
pass: impl EarlyLintPass,
335+
check_node: &ast::Crate,
336336
) -> LintBuffer {
337337
let mut cx = EarlyContextAndPass {
338-
context: EarlyContext::new(
339-
sess,
340-
lint_store,
341-
krate,
342-
crate_attrs,
343-
buffered,
344-
warn_about_weird_lints,
345-
),
338+
context: EarlyContext::new(sess, warn_about_weird_lints, lint_store, crate_attrs, buffered),
346339
pass,
347340
};
348341

349-
// Visit the whole crate.
350-
cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| {
351-
// since the root module isn't visited as an item (because it isn't an
352-
// item), warn for it here.
353-
run_early_pass!(cx, check_crate, krate);
354-
355-
ast_visit::walk_crate(cx, krate);
356-
357-
run_early_pass!(cx, check_crate_post, krate);
342+
cx.with_lint_attrs(ast::CRATE_NODE_ID, &check_node.attrs, |cx| {
343+
run_early_pass!(cx, check_crate, check_node);
344+
ast_visit::walk_crate(cx, check_node);
345+
run_early_pass!(cx, check_crate_post, check_node);
358346
});
359347
cx.context.buffered
360348
}
361349

362-
pub fn check_ast_crate<T: EarlyLintPass>(
350+
pub fn check_ast_node(
363351
sess: &Session,
352+
pre_expansion: bool,
364353
lint_store: &LintStore,
365-
krate: &ast::Crate,
366354
crate_attrs: &[ast::Attribute],
367-
pre_expansion: bool,
368355
lint_buffer: Option<LintBuffer>,
369-
builtin_lints: T,
356+
builtin_lints: impl EarlyLintPass,
357+
check_node: &ast::Crate,
370358
) {
371359
let passes =
372360
if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
373361
let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
374362
let mut buffered = lint_buffer.unwrap_or_default();
375363

376364
if !sess.opts.debugging_opts.no_interleave_lints {
377-
buffered = early_lint_crate(
365+
buffered = early_lint_node(
378366
sess,
367+
pre_expansion,
379368
lint_store,
380-
krate,
381369
crate_attrs,
382-
builtin_lints,
383370
buffered,
384-
pre_expansion,
371+
builtin_lints,
372+
check_node,
385373
);
386374

387375
if !passes.is_empty() {
388-
buffered = early_lint_crate(
376+
buffered = early_lint_node(
389377
sess,
378+
false,
390379
lint_store,
391-
krate,
392380
crate_attrs,
393-
EarlyLintPassObjects { lints: &mut passes[..] },
394381
buffered,
395-
false,
382+
EarlyLintPassObjects { lints: &mut passes[..] },
383+
check_node,
396384
);
397385
}
398386
} else {
399387
for (i, pass) in passes.iter_mut().enumerate() {
400388
buffered =
401389
sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
402-
early_lint_crate(
390+
early_lint_node(
403391
sess,
392+
pre_expansion && i == 0,
404393
lint_store,
405-
krate,
406394
crate_attrs,
407-
EarlyLintPassObjects { lints: slice::from_mut(pass) },
408395
buffered,
409-
pre_expansion && i == 0,
396+
EarlyLintPassObjects { lints: slice::from_mut(pass) },
397+
check_node,
410398
)
411399
});
412400
}

0 commit comments

Comments
 (0)