]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_lint/src/context.rs
Merge tag 'debian/1.49.0+dfsg1-1_exp1' into debian/sid
[rustc.git] / compiler / rustc_lint / src / context.rs
1 //! Implementation of lint checking.
2 //!
3 //! The lint checking is mostly consolidated into one pass which runs
4 //! after all other analyses. Throughout compilation, lint warnings
5 //! can be added via the `add_lint` method on the Session structure. This
6 //! requires a span and an ID of the node that the lint is being added to. The
7 //! lint isn't actually emitted at that time because it is unknown what the
8 //! actual lint level at that location is.
9 //!
10 //! To actually emit lint warnings/errors, a separate pass is used.
11 //! A context keeps track of the current state of all lint levels.
12 //! Upon entering a node of the ast which can modify the lint settings, the
13 //! previous lint state is pushed onto a stack and the ast is then recursed
14 //! upon. As the ast is traversed, this keeps track of the current lint level
15 //! for all lint attributes.
16
17 use self::TargetLint::*;
18
19 use crate::levels::LintLevelsBuilder;
20 use crate::passes::{EarlyLintPassObject, LateLintPassObject};
21 use rustc_ast as ast;
22 use rustc_ast::util::lev_distance::find_best_match_for_name;
23 use rustc_data_structures::fx::FxHashMap;
24 use rustc_data_structures::sync;
25 use rustc_errors::{add_elided_lifetime_in_path_suggestion, struct_span_err, Applicability};
26 use rustc_hir as hir;
27 use rustc_hir::def::Res;
28 use rustc_hir::def_id::{CrateNum, DefId};
29 use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
30 use rustc_middle::lint::LintDiagnosticBuilder;
31 use rustc_middle::middle::privacy::AccessLevels;
32 use rustc_middle::middle::stability;
33 use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
34 use rustc_middle::ty::print::with_no_trimmed_paths;
35 use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
36 use rustc_session::lint::BuiltinLintDiagnostics;
37 use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
38 use rustc_session::Session;
39 use rustc_session::SessionLintStore;
40 use rustc_span::{symbol::Symbol, MultiSpan, Span, DUMMY_SP};
41 use rustc_target::abi::LayoutOf;
42
43 use std::cell::Cell;
44 use std::slice;
45
46 /// Information about the registered lints.
47 ///
48 /// This is basically the subset of `Context` that we can
49 /// build early in the compile pipeline.
50 pub struct LintStore {
51 /// Registered lints.
52 lints: Vec<&'static Lint>,
53
54 /// Constructor functions for each variety of lint pass.
55 ///
56 /// These should only be called once, but since we want to avoid locks or
57 /// interior mutability, we don't enforce this (and lints should, in theory,
58 /// be compatible with being constructed more than once, though not
59 /// necessarily in a sane manner. This is safe though.)
60 pub pre_expansion_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
61 pub early_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
62 pub late_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
63 /// This is unique in that we construct them per-module, so not once.
64 pub late_module_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
65
66 /// Lints indexed by name.
67 by_name: FxHashMap<String, TargetLint>,
68
69 /// Map of registered lint groups to what lints they expand to.
70 lint_groups: FxHashMap<&'static str, LintGroup>,
71 }
72
73 impl SessionLintStore for LintStore {
74 fn name_to_lint(&self, lint_name: &str) -> LintId {
75 let lints = self
76 .find_lints(lint_name)
77 .unwrap_or_else(|_| panic!("Failed to find lint with name `{}`", lint_name));
78
79 if let &[lint] = lints.as_slice() {
80 return lint;
81 } else {
82 panic!("Found mutliple lints with name `{}`: {:?}", lint_name, lints);
83 }
84 }
85 }
86
87 /// The target of the `by_name` map, which accounts for renaming/deprecation.
88 enum TargetLint {
89 /// A direct lint target
90 Id(LintId),
91
92 /// Temporary renaming, used for easing migration pain; see #16545
93 Renamed(String, LintId),
94
95 /// Lint with this name existed previously, but has been removed/deprecated.
96 /// The string argument is the reason for removal.
97 Removed(String),
98 }
99
100 pub enum FindLintError {
101 NotFound,
102 Removed,
103 }
104
105 struct LintAlias {
106 name: &'static str,
107 /// Whether deprecation warnings should be suppressed for this alias.
108 silent: bool,
109 }
110
111 struct LintGroup {
112 lint_ids: Vec<LintId>,
113 from_plugin: bool,
114 depr: Option<LintAlias>,
115 }
116
117 pub enum CheckLintNameResult<'a> {
118 Ok(&'a [LintId]),
119 /// Lint doesn't exist. Potentially contains a suggestion for a correct lint name.
120 NoLint(Option<Symbol>),
121 /// The lint is either renamed or removed. This is the warning
122 /// message, and an optional new name (`None` if removed).
123 Warning(String, Option<String>),
124 /// The lint is from a tool. If the Option is None, then either
125 /// the lint does not exist in the tool or the code was not
126 /// compiled with the tool and therefore the lint was never
127 /// added to the `LintStore`. Otherwise the `LintId` will be
128 /// returned as if it where a rustc lint.
129 Tool(Result<&'a [LintId], (Option<&'a [LintId]>, String)>),
130 }
131
132 impl LintStore {
133 pub fn new() -> LintStore {
134 LintStore {
135 lints: vec![],
136 pre_expansion_passes: vec![],
137 early_passes: vec![],
138 late_passes: vec![],
139 late_module_passes: vec![],
140 by_name: Default::default(),
141 lint_groups: Default::default(),
142 }
143 }
144
145 pub fn get_lints<'t>(&'t self) -> &'t [&'static Lint] {
146 &self.lints
147 }
148
149 pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> {
150 self.lint_groups
151 .iter()
152 .filter(|(_, LintGroup { depr, .. })| {
153 // Don't display deprecated lint groups.
154 depr.is_none()
155 })
156 .map(|(k, LintGroup { lint_ids, from_plugin, .. })| {
157 (*k, lint_ids.clone(), *from_plugin)
158 })
159 .collect()
160 }
161
162 pub fn register_early_pass(
163 &mut self,
164 pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
165 ) {
166 self.early_passes.push(Box::new(pass));
167 }
168
169 pub fn register_pre_expansion_pass(
170 &mut self,
171 pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
172 ) {
173 self.pre_expansion_passes.push(Box::new(pass));
174 }
175
176 pub fn register_late_pass(
177 &mut self,
178 pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
179 ) {
180 self.late_passes.push(Box::new(pass));
181 }
182
183 pub fn register_late_mod_pass(
184 &mut self,
185 pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
186 ) {
187 self.late_module_passes.push(Box::new(pass));
188 }
189
190 // Helper method for register_early/late_pass
191 pub fn register_lints(&mut self, lints: &[&'static Lint]) {
192 for lint in lints {
193 self.lints.push(lint);
194
195 let id = LintId::of(lint);
196 if self.by_name.insert(lint.name_lower(), Id(id)).is_some() {
197 bug!("duplicate specification of lint {}", lint.name_lower())
198 }
199
200 if let Some(FutureIncompatibleInfo { edition, .. }) = lint.future_incompatible {
201 if let Some(edition) = edition {
202 self.lint_groups
203 .entry(edition.lint_name())
204 .or_insert(LintGroup {
205 lint_ids: vec![],
206 from_plugin: lint.is_plugin,
207 depr: None,
208 })
209 .lint_ids
210 .push(id);
211 }
212
213 self.lint_groups
214 .entry("future_incompatible")
215 .or_insert(LintGroup {
216 lint_ids: vec![],
217 from_plugin: lint.is_plugin,
218 depr: None,
219 })
220 .lint_ids
221 .push(id);
222 }
223 }
224 }
225
226 pub fn register_group_alias(&mut self, lint_name: &'static str, alias: &'static str) {
227 self.lint_groups.insert(
228 alias,
229 LintGroup {
230 lint_ids: vec![],
231 from_plugin: false,
232 depr: Some(LintAlias { name: lint_name, silent: true }),
233 },
234 );
235 }
236
237 pub fn register_group(
238 &mut self,
239 from_plugin: bool,
240 name: &'static str,
241 deprecated_name: Option<&'static str>,
242 to: Vec<LintId>,
243 ) {
244 let new = self
245 .lint_groups
246 .insert(name, LintGroup { lint_ids: to, from_plugin, depr: None })
247 .is_none();
248 if let Some(deprecated) = deprecated_name {
249 self.lint_groups.insert(
250 deprecated,
251 LintGroup {
252 lint_ids: vec![],
253 from_plugin,
254 depr: Some(LintAlias { name, silent: false }),
255 },
256 );
257 }
258
259 if !new {
260 bug!("duplicate specification of lint group {}", name);
261 }
262 }
263
264 pub fn register_renamed(&mut self, old_name: &str, new_name: &str) {
265 let target = match self.by_name.get(new_name) {
266 Some(&Id(lint_id)) => lint_id,
267 _ => bug!("invalid lint renaming of {} to {}", old_name, new_name),
268 };
269 self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target));
270 }
271
272 pub fn register_removed(&mut self, name: &str, reason: &str) {
273 self.by_name.insert(name.into(), Removed(reason.into()));
274 }
275
276 pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
277 match self.by_name.get(lint_name) {
278 Some(&Id(lint_id)) => Ok(vec![lint_id]),
279 Some(&Renamed(_, lint_id)) => Ok(vec![lint_id]),
280 Some(&Removed(_)) => Err(FindLintError::Removed),
281 None => loop {
282 return match self.lint_groups.get(lint_name) {
283 Some(LintGroup { lint_ids, depr, .. }) => {
284 if let Some(LintAlias { name, .. }) = depr {
285 lint_name = name;
286 continue;
287 }
288 Ok(lint_ids.clone())
289 }
290 None => Err(FindLintError::Removed),
291 };
292 },
293 }
294 }
295
296 /// Checks the validity of lint names derived from the command line
297 pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
298 let db = match self.check_lint_name(lint_name, None) {
299 CheckLintNameResult::Ok(_) => None,
300 CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
301 CheckLintNameResult::NoLint(suggestion) => {
302 let mut err =
303 struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name);
304
305 if let Some(suggestion) = suggestion {
306 err.help(&format!("did you mean: `{}`", suggestion));
307 }
308
309 Some(err)
310 }
311 CheckLintNameResult::Tool(result) => match result {
312 Err((Some(_), new_name)) => Some(sess.struct_warn(&format!(
313 "lint name `{}` is deprecated \
314 and does not have an effect anymore. \
315 Use: {}",
316 lint_name, new_name
317 ))),
318 _ => None,
319 },
320 };
321
322 if let Some(mut db) = db {
323 let msg = format!(
324 "requested on the command line with `{} {}`",
325 match level {
326 Level::Allow => "-A",
327 Level::Warn => "-W",
328 Level::Deny => "-D",
329 Level::Forbid => "-F",
330 },
331 lint_name
332 );
333 db.note(&msg);
334 db.emit();
335 }
336 }
337
338 /// Checks the name of a lint for its existence, and whether it was
339 /// renamed or removed. Generates a DiagnosticBuilder containing a
340 /// warning for renamed and removed lints. This is over both lint
341 /// names from attributes and those passed on the command line. Since
342 /// it emits non-fatal warnings and there are *two* lint passes that
343 /// inspect attributes, this is only run from the late pass to avoid
344 /// printing duplicate warnings.
345 pub fn check_lint_name(
346 &self,
347 lint_name: &str,
348 tool_name: Option<Symbol>,
349 ) -> CheckLintNameResult<'_> {
350 let complete_name = if let Some(tool_name) = tool_name {
351 format!("{}::{}", tool_name, lint_name)
352 } else {
353 lint_name.to_string()
354 };
355 // If the lint was scoped with `tool::` check if the tool lint exists
356 if tool_name.is_some() {
357 match self.by_name.get(&complete_name) {
358 None => match self.lint_groups.get(&*complete_name) {
359 None => return CheckLintNameResult::Tool(Err((None, String::new()))),
360 Some(LintGroup { lint_ids, .. }) => {
361 return CheckLintNameResult::Tool(Ok(&lint_ids));
362 }
363 },
364 Some(&Id(ref id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))),
365 // If the lint was registered as removed or renamed by the lint tool, we don't need
366 // to treat tool_lints and rustc lints different and can use the code below.
367 _ => {}
368 }
369 }
370 match self.by_name.get(&complete_name) {
371 Some(&Renamed(ref new_name, _)) => CheckLintNameResult::Warning(
372 format!("lint `{}` has been renamed to `{}`", complete_name, new_name),
373 Some(new_name.to_owned()),
374 ),
375 Some(&Removed(ref reason)) => CheckLintNameResult::Warning(
376 format!("lint `{}` has been removed: `{}`", complete_name, reason),
377 None,
378 ),
379 None => match self.lint_groups.get(&*complete_name) {
380 // If neither the lint, nor the lint group exists check if there is a `clippy::`
381 // variant of this lint
382 None => self.check_tool_name_for_backwards_compat(&complete_name, "clippy"),
383 Some(LintGroup { lint_ids, depr, .. }) => {
384 // Check if the lint group name is deprecated
385 if let Some(LintAlias { name, silent }) = depr {
386 let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
387 return if *silent {
388 CheckLintNameResult::Ok(&lint_ids)
389 } else {
390 CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
391 };
392 }
393 CheckLintNameResult::Ok(&lint_ids)
394 }
395 },
396 Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
397 }
398 }
399
400 fn check_tool_name_for_backwards_compat(
401 &self,
402 lint_name: &str,
403 tool_name: &str,
404 ) -> CheckLintNameResult<'_> {
405 let complete_name = format!("{}::{}", tool_name, lint_name);
406 match self.by_name.get(&complete_name) {
407 None => match self.lint_groups.get(&*complete_name) {
408 // Now we are sure, that this lint exists nowhere
409 None => {
410 let symbols =
411 self.by_name.keys().map(|name| Symbol::intern(&name)).collect::<Vec<_>>();
412
413 let suggestion = find_best_match_for_name(
414 symbols.iter(),
415 Symbol::intern(&lint_name.to_lowercase()),
416 None,
417 );
418
419 CheckLintNameResult::NoLint(suggestion)
420 }
421 Some(LintGroup { lint_ids, depr, .. }) => {
422 // Reaching this would be weird, but let's cover this case anyway
423 if let Some(LintAlias { name, silent }) = depr {
424 let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
425 return if *silent {
426 CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
427 } else {
428 CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
429 };
430 }
431 CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
432 }
433 },
434 Some(&Id(ref id)) => {
435 CheckLintNameResult::Tool(Err((Some(slice::from_ref(id)), complete_name)))
436 }
437 _ => CheckLintNameResult::NoLint(None),
438 }
439 }
440 }
441
442 /// Context for lint checking after type checking.
443 pub struct LateContext<'tcx> {
444 /// Type context we're checking in.
445 pub tcx: TyCtxt<'tcx>,
446
447 /// Current body, or `None` if outside a body.
448 pub enclosing_body: Option<hir::BodyId>,
449
450 /// Type-checking results for the current body. Access using the `typeck_results`
451 /// and `maybe_typeck_results` methods, which handle querying the typeck results on demand.
452 // FIXME(eddyb) move all the code accessing internal fields like this,
453 // to this module, to avoid exposing it to lint logic.
454 pub(super) cached_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
455
456 /// Parameter environment for the item we are in.
457 pub param_env: ty::ParamEnv<'tcx>,
458
459 /// Items accessible from the crate being checked.
460 pub access_levels: &'tcx AccessLevels,
461
462 /// The store of registered lints and the lint levels.
463 pub lint_store: &'tcx LintStore,
464
465 pub last_node_with_lint_attrs: hir::HirId,
466
467 /// Generic type parameters in scope for the item we are in.
468 pub generics: Option<&'tcx hir::Generics<'tcx>>,
469
470 /// We are only looking at one module
471 pub only_module: bool,
472 }
473
474 /// Context for lint checking of the AST, after expansion, before lowering to
475 /// HIR.
476 pub struct EarlyContext<'a> {
477 /// Type context we're checking in.
478 pub sess: &'a Session,
479
480 /// The crate being checked.
481 pub krate: &'a ast::Crate,
482
483 pub builder: LintLevelsBuilder<'a>,
484
485 /// The store of registered lints and the lint levels.
486 pub lint_store: &'a LintStore,
487
488 pub buffered: LintBuffer,
489 }
490
491 pub trait LintPassObject: Sized {}
492
493 impl LintPassObject for EarlyLintPassObject {}
494
495 impl LintPassObject for LateLintPassObject {}
496
497 pub trait LintContext: Sized {
498 type PassObject: LintPassObject;
499
500 fn sess(&self) -> &Session;
501 fn lints(&self) -> &LintStore;
502
503 fn lookup_with_diagnostics(
504 &self,
505 lint: &'static Lint,
506 span: Option<impl Into<MultiSpan>>,
507 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
508 diagnostic: BuiltinLintDiagnostics,
509 ) {
510 self.lookup(lint, span, |lint| {
511 // We first generate a blank diagnostic.
512 let mut db = lint.build("");
513
514 // Now, set up surrounding context.
515 let sess = self.sess();
516 match diagnostic {
517 BuiltinLintDiagnostics::Normal => (),
518 BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
519 let (sugg, app) = match sess.source_map().span_to_snippet(span) {
520 Ok(s) if is_global => {
521 (format!("dyn ({})", s), Applicability::MachineApplicable)
522 }
523 Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
524 Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
525 };
526 db.span_suggestion(span, "use `dyn`", sugg, app);
527 }
528 BuiltinLintDiagnostics::AbsPathWithModule(span) => {
529 let (sugg, app) = match sess.source_map().span_to_snippet(span) {
530 Ok(ref s) => {
531 // FIXME(Manishearth) ideally the emitting code
532 // can tell us whether or not this is global
533 let opt_colon =
534 if s.trim_start().starts_with("::") { "" } else { "::" };
535
536 (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
537 }
538 Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
539 };
540 db.span_suggestion(span, "use `crate`", sugg, app);
541 }
542 BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
543 db.span_label(
544 span,
545 "names from parent modules are not accessible without an explicit import",
546 );
547 }
548 BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(
549 span_def,
550 ) => {
551 db.span_note(span_def, "the macro is defined here");
552 }
553 BuiltinLintDiagnostics::ElidedLifetimesInPaths(
554 n,
555 path_span,
556 incl_angl_brckt,
557 insertion_span,
558 anon_lts,
559 ) => {
560 add_elided_lifetime_in_path_suggestion(
561 sess.source_map(),
562 &mut db,
563 n,
564 path_span,
565 incl_angl_brckt,
566 insertion_span,
567 anon_lts,
568 );
569 }
570 BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
571 db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
572 }
573 BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
574 if !replaces.is_empty() {
575 db.tool_only_multipart_suggestion(
576 &message,
577 replaces,
578 Applicability::MachineApplicable,
579 );
580 }
581 }
582 BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
583 for (span, is_imported) in spans {
584 let introduced = if is_imported { "imported" } else { "defined" };
585 db.span_label(
586 span,
587 format!("the item `{}` is already {} here", ident, introduced),
588 );
589 }
590 }
591 BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
592 stability::deprecation_suggestion(&mut db, "macro", suggestion, span)
593 }
594 BuiltinLintDiagnostics::UnusedDocComment(span) => {
595 db.span_label(span, "rustdoc does not generate documentation for macro invocations");
596 db.help("to document an item produced by a macro, \
597 the macro must produce the documentation as part of its expansion");
598 }
599 }
600 // Rewrap `db`, and pass control to the user.
601 decorate(LintDiagnosticBuilder::new(db));
602 });
603 }
604
605 // FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
606 // set the span in their `decorate` function (preferably using set_span).
607 fn lookup<S: Into<MultiSpan>>(
608 &self,
609 lint: &'static Lint,
610 span: Option<S>,
611 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
612 );
613
614 fn struct_span_lint<S: Into<MultiSpan>>(
615 &self,
616 lint: &'static Lint,
617 span: S,
618 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
619 ) {
620 self.lookup(lint, Some(span), decorate);
621 }
622 /// Emit a lint at the appropriate level, with no associated span.
623 fn lint(&self, lint: &'static Lint, decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>)) {
624 self.lookup(lint, None as Option<Span>, decorate);
625 }
626 }
627
628 impl<'a> EarlyContext<'a> {
629 pub fn new(
630 sess: &'a Session,
631 lint_store: &'a LintStore,
632 krate: &'a ast::Crate,
633 buffered: LintBuffer,
634 warn_about_weird_lints: bool,
635 ) -> EarlyContext<'a> {
636 EarlyContext {
637 sess,
638 krate,
639 lint_store,
640 builder: LintLevelsBuilder::new(sess, warn_about_weird_lints, lint_store),
641 buffered,
642 }
643 }
644 }
645
646 impl LintContext for LateContext<'_> {
647 type PassObject = LateLintPassObject;
648
649 /// Gets the overall compiler `Session` object.
650 fn sess(&self) -> &Session {
651 &self.tcx.sess
652 }
653
654 fn lints(&self) -> &LintStore {
655 &*self.lint_store
656 }
657
658 fn lookup<S: Into<MultiSpan>>(
659 &self,
660 lint: &'static Lint,
661 span: Option<S>,
662 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
663 ) {
664 let hir_id = self.last_node_with_lint_attrs;
665
666 match span {
667 Some(s) => self.tcx.struct_span_lint_hir(lint, hir_id, s, decorate),
668 None => self.tcx.struct_lint_node(lint, hir_id, decorate),
669 }
670 }
671 }
672
673 impl LintContext for EarlyContext<'_> {
674 type PassObject = EarlyLintPassObject;
675
676 /// Gets the overall compiler `Session` object.
677 fn sess(&self) -> &Session {
678 &self.sess
679 }
680
681 fn lints(&self) -> &LintStore {
682 &*self.lint_store
683 }
684
685 fn lookup<S: Into<MultiSpan>>(
686 &self,
687 lint: &'static Lint,
688 span: Option<S>,
689 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
690 ) {
691 self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
692 }
693 }
694
695 impl<'tcx> LateContext<'tcx> {
696 /// Gets the type-checking results for the current body,
697 /// or `None` if outside a body.
698 pub fn maybe_typeck_results(&self) -> Option<&'tcx ty::TypeckResults<'tcx>> {
699 self.cached_typeck_results.get().or_else(|| {
700 self.enclosing_body.map(|body| {
701 let typeck_results = self.tcx.typeck_body(body);
702 self.cached_typeck_results.set(Some(typeck_results));
703 typeck_results
704 })
705 })
706 }
707
708 /// Gets the type-checking results for the current body.
709 /// As this will ICE if called outside bodies, only call when working with
710 /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
711 #[track_caller]
712 pub fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
713 self.maybe_typeck_results().expect("`LateContext::typeck_results` called outside of body")
714 }
715
716 /// Returns the final resolution of a `QPath`, or `Res::Err` if unavailable.
717 /// Unlike `.typeck_results().qpath_res(qpath, id)`, this can be used even outside
718 /// bodies (e.g. for paths in `hir::Ty`), without any risk of ICE-ing.
719 pub fn qpath_res(&self, qpath: &hir::QPath<'_>, id: hir::HirId) -> Res {
720 match *qpath {
721 hir::QPath::Resolved(_, ref path) => path.res,
722 hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self
723 .maybe_typeck_results()
724 .and_then(|typeck_results| typeck_results.type_dependent_def(id))
725 .map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
726 }
727 }
728
729 /// Check if a `DefId`'s path matches the given absolute type path usage.
730 ///
731 /// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
732 /// inherent `impl` blocks are matched with the name of the type.
733 ///
734 /// Instead of using this method, it is often preferable to instead use
735 /// `rustc_diagnostic_item` or a `lang_item`. This is less prone to errors
736 /// as paths get invalidated if the target definition moves.
737 ///
738 /// # Examples
739 ///
740 /// ```rust,ignore (no context or def id available)
741 /// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
742 /// // The given `def_id` is that of an `Option` type
743 /// }
744 /// ```
745 pub fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
746 let names = self.get_def_path(def_id);
747
748 names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| a == b)
749 }
750
751 /// Gets the absolute path of `def_id` as a vector of `Symbol`.
752 ///
753 /// # Examples
754 ///
755 /// ```rust,ignore (no context or def id available)
756 /// let def_path = cx.get_def_path(def_id);
757 /// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
758 /// // The given `def_id` is that of an `Option` type
759 /// }
760 /// ```
761 pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
762 pub struct AbsolutePathPrinter<'tcx> {
763 pub tcx: TyCtxt<'tcx>,
764 }
765
766 impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
767 type Error = !;
768
769 type Path = Vec<Symbol>;
770 type Region = ();
771 type Type = ();
772 type DynExistential = ();
773 type Const = ();
774
775 fn tcx(&self) -> TyCtxt<'tcx> {
776 self.tcx
777 }
778
779 fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
780 Ok(())
781 }
782
783 fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
784 Ok(())
785 }
786
787 fn print_dyn_existential(
788 self,
789 _predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
790 ) -> Result<Self::DynExistential, Self::Error> {
791 Ok(())
792 }
793
794 fn print_const(self, _ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
795 Ok(())
796 }
797
798 fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
799 Ok(vec![self.tcx.original_crate_name(cnum)])
800 }
801
802 fn path_qualified(
803 self,
804 self_ty: Ty<'tcx>,
805 trait_ref: Option<ty::TraitRef<'tcx>>,
806 ) -> Result<Self::Path, Self::Error> {
807 if trait_ref.is_none() {
808 if let ty::Adt(def, substs) = self_ty.kind() {
809 return self.print_def_path(def.did, substs);
810 }
811 }
812
813 // This shouldn't ever be needed, but just in case:
814 with_no_trimmed_paths(|| {
815 Ok(vec![match trait_ref {
816 Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
817 None => Symbol::intern(&format!("<{}>", self_ty)),
818 }])
819 })
820 }
821
822 fn path_append_impl(
823 self,
824 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
825 _disambiguated_data: &DisambiguatedDefPathData,
826 self_ty: Ty<'tcx>,
827 trait_ref: Option<ty::TraitRef<'tcx>>,
828 ) -> Result<Self::Path, Self::Error> {
829 let mut path = print_prefix(self)?;
830
831 // This shouldn't ever be needed, but just in case:
832 path.push(match trait_ref {
833 Some(trait_ref) => with_no_trimmed_paths(|| {
834 Symbol::intern(&format!(
835 "<impl {} for {}>",
836 trait_ref.print_only_trait_path(),
837 self_ty
838 ))
839 }),
840 None => {
841 with_no_trimmed_paths(|| Symbol::intern(&format!("<impl {}>", self_ty)))
842 }
843 });
844
845 Ok(path)
846 }
847
848 fn path_append(
849 self,
850 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
851 disambiguated_data: &DisambiguatedDefPathData,
852 ) -> Result<Self::Path, Self::Error> {
853 let mut path = print_prefix(self)?;
854
855 // Skip `::{{constructor}}` on tuple/unit structs.
856 if let DefPathData::Ctor = disambiguated_data.data {
857 return Ok(path);
858 }
859
860 path.push(Symbol::intern(&disambiguated_data.data.to_string()));
861 Ok(path)
862 }
863
864 fn path_generic_args(
865 self,
866 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
867 _args: &[GenericArg<'tcx>],
868 ) -> Result<Self::Path, Self::Error> {
869 print_prefix(self)
870 }
871 }
872
873 AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
874 }
875 }
876
877 impl<'tcx> LayoutOf for LateContext<'tcx> {
878 type Ty = Ty<'tcx>;
879 type TyAndLayout = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
880
881 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
882 self.tcx.layout_of(self.param_env.and(ty))
883 }
884 }