Skip to content

Commit abd9dca

Browse files
committed
Move from symbols to strings + fix things
1 parent 0724b8d commit abd9dca

File tree

5 files changed

+86
-47
lines changed

5 files changed

+86
-47
lines changed

compiler/rustc_lint/src/late.rs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -372,35 +372,37 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
372372
store.late_module_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
373373

374374
// Filter unused lints
375-
let lints_allowed = &**tcx.lints_that_can_emit(());
375+
let (lints_to_emit, lints_allowed) = &**tcx.lints_that_can_emit(());
376376
// let lints_to_emit = &lints_that_can_emit.0;
377377
// let lints_allowed = &lints_that_can_emit.1;
378378

379379
// Now, we'll filtered passes in a way that discards any lint that
380380
let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes
381-
.into_iter()
382-
.filter(|pass| {
383-
let pass = LintPass::get_lints(pass);
384-
pass.iter().any(|&lint| {
385-
let lint_name = &lint.name.to_lowercase()
381+
.into_iter()
382+
.filter(|pass| {
383+
let pass = LintPass::get_lints(pass);
384+
pass.iter().any(|&lint| {
385+
let lint_name =
386+
&lint.name.to_lowercase()
386387
// Doing some calculations here to account for those separators
387-
[lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..];
388-
!lints_allowed.contains(&lint_name.to_string())
389-
&& lint.default_level != crate::Level::Allow
390-
})
391-
// if passes_lints[i].iter().any(|&lint| {
392-
// let symbol =
393-
// &lint.name.to_lowercase()
394-
// // Doing some calculations here to account for those separators
395-
// [lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..]
396-
397-
// // ^^^ Expensive, but more expensive would be having to
398-
// // cast every element to &str
399-
400-
// (!lints_allowed.contains(&lint.name)
401-
// && lint.default_level != crate::Level::Allow)
388+
[lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..].to_string();
389+
lints_to_emit.contains(&lint_name) || ( !lints_allowed.contains(&lint_name) && lint.default_level != crate::Level::Allow )
390+
})
391+
// if passes_lints[i].iter().any(|&lint| {
392+
// let symbol =
393+
// &lint.name.to_lowercase()
394+
// // Doing some calculations here to account for those separators
395+
// [lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..]
396+
397+
// // ^^^ Expensive, but more expensive would be having to
398+
// // cast every element to &str
399+
400+
401+
402+
// (!lints_allowed.contains(&lint.name)
403+
// && lint.default_level != crate::Level::Allow)
402404
})
403-
.collect();
405+
.collect();
404406

405407
filtered_passes.push(Box::new(builtin_lints));
406408

@@ -453,24 +455,35 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
453455
only_module: false,
454456
};
455457

456-
let lints_allowed = &**tcx.lints_that_can_emit(());
457-
// let lints_to_emit = &lints_that_can_emit.0;
458-
// let lints_allowed = &lints_that_can_emit.1;
458+
let (lints_to_emit, lints_allowed) = &**tcx.lints_that_can_emit(());
459459

460460
// Now, we'll filtered passes in a way that discards any lint that
461461
let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes
462462
.into_iter()
463463
.filter(|pass| {
464464
let pass = LintPass::get_lints(pass);
465465
pass.iter().any(|&lint| {
466-
let lint_name = &lint.name.to_lowercase()
466+
let lint_name =
467+
&lint.name.to_lowercase()
467468
// Doing some calculations here to account for those separators
468-
[lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..];
469-
!lints_allowed.contains(&lint_name.to_string())
470-
&& lint.default_level != crate::Level::Allow
469+
[lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..].to_string();
470+
lints_to_emit.contains(&lint_name) ||
471+
(!lints_allowed.contains(lint_name) && lint.default_level != crate::Level::Allow)
471472
})
472-
})
473-
.collect();
473+
// if passes_lints[i].iter().any(|&lint| {
474+
// let symbol =
475+
// &lint.name.to_lowercase()
476+
// // Doing some calculations here to account for those separators
477+
// [lint.name.find("::").unwrap_or(lint.name.len() - 2) + 2..]
478+
479+
// // ^^^ Expensive, but more expensive would be having to
480+
// // cast every element to &str
481+
482+
483+
484+
// (!lints_allowed.contains(&lint.name)
485+
// && lint.default_level != crate::Level::Allow)
486+
}).collect();
474487

475488
let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] };
476489
late_lint_crate_inner(tcx, context, pass);

compiler/rustc_lint/src/levels.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_ast as ast;
1717
use rustc_ast_pretty::pprust;
1818
use rustc_data_structures::{
1919
fx::FxIndexMap,
20-
sync::{par_for_each_in, Lock},
20+
sync::{join, par_for_each_in, Lock, Lrc},
2121
};
2222
use rustc_errors::{Diag, DiagMessage, LintDiagnostic, MultiSpan};
2323
use rustc_feature::{Features, GateIssue};
@@ -159,12 +159,12 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
159159
/// (and not allowed in the crate) and CLI lints. The returned value is a tuple
160160
/// of 1. The lints that will emit (or at least, should run), and 2.
161161
/// The lints that are allowed at the crate level and will not emit.
162-
pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Vec<String> {
162+
pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(Vec<String>, Vec<String>)> {
163163
let mut visitor = LintLevelMinimum::new(tcx);
164164
visitor.process_opts();
165165
visitor.lint_level_minimums();
166166

167-
visitor.lints_allowed.into_inner()
167+
Lrc::new((visitor.lints_to_emit.into_inner(), visitor.lints_allowed.into_inner()))
168168
}
169169

170170
#[instrument(level = "trace", skip(tcx), ret)]
@@ -474,29 +474,51 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'
474474
struct LintLevelMinimum<'tcx> {
475475
tcx: TyCtxt<'tcx>,
476476
/// The actual list of detected lints.
477+
lints_to_emit: Lock<Vec<String>>,
477478
lints_allowed: Lock<Vec<String>>,
478479
}
479480

480481
impl<'tcx> LintLevelMinimum<'tcx> {
481482
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
482-
Self { tcx, lints_allowed: Lock::new(Vec::with_capacity(100)) }
483+
Self {
484+
tcx,
485+
// That magic number is the current number of lints + some more for possible future lints
486+
lints_to_emit: Lock::new(Vec::with_capacity(230)),
487+
lints_allowed: Lock::new(Vec::with_capacity(100)),
488+
}
483489
}
484490

485491
fn process_opts(&mut self) {
486492
for (lint, level) in &self.tcx.sess.opts.lint_opts {
487493
if *level == Level::Allow {
488-
self.lints_allowed.with_lock(|lints_allowed| lints_allowed.push(lint.to_string()));
494+
self.lints_allowed
495+
.with_lock(|lints_allowed| lints_allowed.push(lint.to_string()));
496+
} else {
497+
self.lints_to_emit
498+
.with_lock(|lints_to_emit| lints_to_emit.push(lint.to_string()));
489499
}
490500
}
491501
}
492502

493503
fn lint_level_minimums(&mut self) {
494-
self.tcx.sess.psess.lints_allowed.with_lock(|vec| {
495-
par_for_each_in(vec, |lint_symbol| {
496-
self.lints_allowed
497-
.with_lock(|lints_allowed| lints_allowed.push(lint_symbol.to_string()));
498-
});
499-
});
504+
join(
505+
|| {
506+
self.tcx.sess.psess.lints_that_can_emit.with_lock(|vec| {
507+
par_for_each_in(vec, |lint_symbol| {
508+
self.lints_to_emit
509+
.with_lock(|lints_to_emit| lints_to_emit.push(lint_symbol.to_string()));
510+
});
511+
});
512+
},
513+
|| {
514+
self.tcx.sess.psess.lints_allowed.with_lock(|vec| {
515+
par_for_each_in(vec, |lint_symbol| {
516+
self.lints_allowed
517+
.with_lock(|lints_allowed| lints_allowed.push(lint_symbol.to_string()));
518+
});
519+
});
520+
},
521+
);
500522
}
501523
}
502524

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ rustc_queries! {
431431
desc { "computing `#[expect]`ed lints in this crate" }
432432
}
433433

434-
query lints_that_can_emit(_: ()) -> &'tcx Vec<String> {
434+
query lints_that_can_emit(_: ()) -> &'tcx Lrc<(Vec<String>, Vec<String>)> {
435435
arena_cache
436436
desc { "Computing all lints that are explicitly enabled or with a default level great than Allow" }
437437
}

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ impl<'a> Parser<'a> {
148148
if let ast::NestedMetaItem::MetaItem(ref meta_item) = meta_list {
149149
if meta_item.path.segments.len() == 1 {
150150
self.psess.lints_that_can_emit.with_lock(|lints_that_can_emit| {
151-
lints_that_can_emit.push(meta_list.ident().unwrap().name);
151+
lints_that_can_emit.push(meta_list.ident().unwrap().name.as_str().to_string());
152152
})
153153
} else {
154154
self.psess.lints_that_can_emit.with_lock(|lints_that_can_emit| {
155-
lints_that_can_emit.push(meta_item.path.segments[1].ident.name);
155+
lints_that_can_emit.push(meta_item.path.segments[1].ident.name.as_str().to_string());
156156
})
157157
}
158158
}
@@ -163,11 +163,11 @@ impl<'a> Parser<'a> {
163163
if let ast::NestedMetaItem::MetaItem(ref meta_item) = meta_list {
164164
if meta_item.path.segments.len() == 1 {
165165
self.psess.lints_allowed.with_lock(|lints_allowed| {
166-
lints_allowed.push(meta_list.name_or_empty())
166+
lints_allowed.push(meta_list.name_or_empty().as_str().to_string())
167167
})
168168
} else {
169169
self.psess.lints_allowed.with_lock(|lints_allowed| {
170-
lints_allowed.push(meta_item.path.segments[1].ident.name);
170+
lints_allowed.push(meta_item.path.segments[1].ident.name.as_str().to_string());
171171
})
172172
}
173173
}

compiler/rustc_session/src/parse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ pub struct ParseSess {
232232
proc_macro_quoted_spans: AppendOnlyVec<Span>,
233233
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
234234
pub attr_id_generator: AttrIdGenerator,
235+
/// All lints that can emit, even if not emitted (i.e. lints that are mentioned
236+
/// in e.g. #![warn] attributes)
237+
pub lints_that_can_emit: Lock<Vec<String>>,
235238
/// The list of lints that cannot emit, maybe because they are allowed
236239
/// globally, or the default level is Allow and they are not activated
237240
/// manually
@@ -270,6 +273,7 @@ impl ParseSess {
270273
assume_incomplete_release: false,
271274
proc_macro_quoted_spans: Default::default(),
272275
attr_id_generator: AttrIdGenerator::new(),
276+
lints_that_can_emit: Lock::new(Vec::with_capacity(230)),
273277
lints_allowed: Lock::new(Vec::with_capacity(100)),
274278
}
275279
}

0 commit comments

Comments
 (0)