3 use crate::ich::StableHashingContext;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6 use rustc_errors::{pluralize, Applicability, DiagnosticBuilder, DiagnosticId};
8 pub use rustc_session::lint::{builtin, Level, Lint, LintId, LintPass};
9 use rustc_session::{DiagnosticMessageId, Session};
10 use rustc_span::hygiene::MacroKind;
11 use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
12 use rustc_span::{Span, Symbol};
14 /// How a lint level was set.
15 #[derive(Clone, Copy, PartialEq, Eq, HashStable)]
17 /// Lint is at the default level as declared
18 /// in rustc or a plugin.
21 /// Lint level was set by an attribute.
22 Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
24 /// Lint level was set by a command-line flag.
28 pub type LevelSource = (Level, LintSource);
30 pub struct LintLevelSets {
31 pub list: Vec<LintSet>,
37 // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
39 specs: FxHashMap<LintId, LevelSource>,
43 specs: FxHashMap<LintId, LevelSource>,
49 pub fn new() -> Self {
50 LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
53 pub fn get_lint_level(
57 aux: Option<&FxHashMap<LintId, LevelSource>>,
60 let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
62 // If `level` is none then we actually assume the default level for this
64 let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
66 // If we're about to issue a warning, check at the last minute for any
67 // directives against the warnings "lint". If, for example, there's an
68 // `allow(warnings)` in scope then we want to respect that instead.
69 if level == Level::Warn {
70 let (warnings_level, warnings_src) =
71 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
72 if let Some(configured_warning_level) = warnings_level {
73 if configured_warning_level != Level::Warn {
74 level = configured_warning_level;
80 // Ensure that we never exceed the `--cap-lints` argument.
81 level = cmp::min(level, self.lint_cap);
83 if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
84 // Ensure that we never exceed driver level.
85 level = cmp::min(*driver_level, level);
91 pub fn get_lint_id_level(
95 aux: Option<&FxHashMap<LintId, LevelSource>>,
96 ) -> (Option<Level>, LintSource) {
97 if let Some(specs) = aux {
98 if let Some(&(level, src)) = specs.get(&id) {
99 return (Some(level), src);
103 match self.list[idx as usize] {
104 LintSet::CommandLine { ref specs } => {
105 if let Some(&(level, src)) = specs.get(&id) {
106 return (Some(level), src);
108 return (None, LintSource::Default);
110 LintSet::Node { ref specs, parent } => {
111 if let Some(&(level, src)) = specs.get(&id) {
112 return (Some(level), src);
121 pub struct LintLevelMap {
122 pub sets: LintLevelSets,
123 pub id_to_set: FxHashMap<HirId, u32>,
127 /// If the `id` was previously registered with `register_id` when building
128 /// this `LintLevelMap` this returns the corresponding lint level and source
129 /// of the lint level for the lint provided.
131 /// If the `id` was not previously registered, returns `None`. If `None` is
132 /// returned then the parent of `id` should be acquired and this function
133 /// should be called again.
134 pub fn level_and_source(
139 ) -> Option<LevelSource> {
140 self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
144 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
146 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
147 let LintLevelMap { ref sets, ref id_to_set } = *self;
149 id_to_set.hash_stable(hcx, hasher);
151 let LintLevelSets { ref list, lint_cap } = *sets;
153 lint_cap.hash_stable(hcx, hasher);
155 hcx.while_hashing_spans(true, |hcx| {
156 list.len().hash_stable(hcx, hasher);
158 // We are working under the assumption here that the list of
159 // lint-sets is built in a deterministic order.
160 for lint_set in list {
161 ::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
164 LintSet::CommandLine { ref specs } => {
165 specs.hash_stable(hcx, hasher);
167 LintSet::Node { ref specs, parent } => {
168 specs.hash_stable(hcx, hasher);
169 parent.hash_stable(hcx, hasher);
177 pub struct LintDiagnosticBuilder<'a>(DiagnosticBuilder<'a>);
179 impl<'a> LintDiagnosticBuilder<'a> {
180 /// Return the inner DiagnosticBuilder, first setting the primary message to `msg`.
181 pub fn build(mut self, msg: &str) -> DiagnosticBuilder<'a> {
182 self.0.set_primary_message(msg);
186 /// Create a LintDiagnosticBuilder from some existing DiagnosticBuilder.
187 pub fn new(err: DiagnosticBuilder<'a>) -> LintDiagnosticBuilder<'a> {
188 LintDiagnosticBuilder(err)
192 pub fn struct_lint_level<'s, 'd>(
197 span: Option<MultiSpan>,
198 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>) + 'd,
200 // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
202 fn struct_lint_level_impl(
207 span: Option<MultiSpan>,
208 decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b>) + 'd>,
210 let mut err = match (level, span) {
211 (Level::Allow, _) => {
214 (Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
215 (Level::Warn, None) => sess.struct_warn(""),
216 (Level::Deny, Some(span)) | (Level::Forbid, Some(span)) => {
217 sess.struct_span_err(span, "")
219 (Level::Deny, None) | (Level::Forbid, None) => sess.struct_err(""),
222 // Check for future incompatibility lints and issue a stronger warning.
223 let lint_id = LintId::of(lint);
224 let future_incompatible = lint.future_incompatible;
226 // If this code originates in a foreign macro, aka something that this crate
227 // did not itself author, then it's likely that there's nothing this crate
228 // can do about it. We probably want to skip the lint entirely.
229 if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
230 // Any suggestions made here are likely to be incorrect, so anything we
231 // emit shouldn't be automatically fixed by rustfix.
232 err.allow_suggestions(false);
234 // If this is a future incompatible lint it'll become a hard error, so
235 // we have to emit *something*. Also allow lints to whitelist themselves
236 // on a case-by-case basis for emission in a foreign macro.
237 if future_incompatible.is_none() && !lint.report_in_external_macro {
239 // Don't continue further, since we don't want to have
240 // `diag_span_note_once` called for a diagnostic that isn't emitted.
245 let name = lint.name_lower();
247 LintSource::Default => {
250 DiagnosticMessageId::from(lint),
251 &format!("`#[{}({})]` on by default", level.as_str(), name),
254 LintSource::CommandLine(lint_flag_val) => {
255 let flag = match level {
258 Level::Forbid => "-F",
259 Level::Allow => panic!(),
261 let hyphen_case_lint_name = name.replace("_", "-");
262 if lint_flag_val.as_str() == name {
265 DiagnosticMessageId::from(lint),
267 "requested on the command line with `{} {}`",
268 flag, hyphen_case_lint_name
272 let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
275 DiagnosticMessageId::from(lint),
277 "`{} {}` implied by `{} {}`",
278 flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
283 LintSource::Node(lint_attr_name, src, reason) => {
284 if let Some(rationale) = reason {
285 err.note(&rationale.as_str());
287 sess.diag_span_note_once(
289 DiagnosticMessageId::from(lint),
291 "the lint level is defined here",
293 if lint_attr_name.as_str() != name {
294 let level_str = level.as_str();
297 DiagnosticMessageId::from(lint),
299 "`#[{}({})]` implied by `#[{}({})]`",
300 level_str, name, level_str, lint_attr_name
307 err.code(DiagnosticId::Lint(name));
309 if let Some(future_incompatible) = future_incompatible {
310 const STANDARD_MESSAGE: &str = "this was previously accepted by the compiler but is being phased out; \
311 it will become a hard error";
313 let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
314 "once this method is added to the standard library, \
315 the ambiguity may cause an error or change in behavior!"
317 } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
318 "this borrowing pattern was not meant to be accepted, \
319 and may become a hard error in the future"
321 } else if let Some(edition) = future_incompatible.edition {
322 format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
324 format!("{} in a future release!", STANDARD_MESSAGE)
326 let citation = format!("for more information, see {}", future_incompatible.reference);
327 err.warn(&explanation);
331 // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
332 decorate(LintDiagnosticBuilder::new(err));
334 struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
337 /// Returns whether `span` originates in a foreign crate's external macro.
339 /// This is used to test whether a lint should not even begin to figure out whether it should
340 /// be reported on the current node.
341 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
342 let expn_data = span.ctxt().outer_expn_data();
343 match expn_data.kind {
344 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
345 ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
346 ExpnKind::Macro(MacroKind::Bang, _) => {
347 if expn_data.def_site.is_dummy() {
348 // Dummy span for the `def_site` means it's an external macro.
351 match sess.source_map().span_to_snippet(expn_data.def_site) {
352 Ok(code) => !code.starts_with("macro_rules"),
353 // No snippet means external macro or compiler-builtin expansion.
357 ExpnKind::Macro(..) => true, // definitely a plugin
361 pub fn add_elided_lifetime_in_path_suggestion(
363 db: &mut DiagnosticBuilder<'_>,
366 incl_angl_brckt: bool,
367 insertion_span: Span,
370 let (replace_span, suggestion) = if incl_angl_brckt {
371 (insertion_span, anon_lts)
373 // When possible, prefer a suggestion that replaces the whole
374 // `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
375 // at a point (which makes for an ugly/confusing label)
376 if let Ok(snippet) = sess.source_map().span_to_snippet(path_span) {
377 // But our spans can get out of whack due to macros; if the place we think
378 // we want to insert `'_` isn't even within the path expression's span, we
379 // should bail out of making any suggestion rather than panicking on a
380 // subtract-with-overflow or string-slice-out-out-bounds (!)
381 // FIXME: can we do better?
382 if insertion_span.lo().0 < path_span.lo().0 {
385 let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize;
386 if insertion_index > snippet.len() {
389 let (before, after) = snippet.split_at(insertion_index);
390 (path_span, format!("{}{}{}", before, anon_lts, after))
392 (insertion_span, anon_lts)
397 &format!("indicate the anonymous lifetime{}", pluralize!(n)),
399 Applicability::MachineApplicable,