/compiler/rustc_passes/src/diagnostic_items.rs

https://gitlab.com/rust-lang/rust · Rust · 113 lines · 79 code · 16 blank · 18 comment · 15 complexity · 7f3395342b716eb05ae0a1654ad15810 MD5 · raw file

  1. //! Detecting diagnostic items.
  2. //!
  3. //! Diagnostic items are items that are not language-inherent, but can reasonably be expected to
  4. //! exist for diagnostic purposes. This allows diagnostic authors to refer to specific items
  5. //! directly, without having to guess module paths and crates.
  6. //! Examples are:
  7. //!
  8. //! * Traits like `Debug`, that have no bearing on language semantics
  9. //!
  10. //! * Compiler internal types like `Ty` and `TyCtxt`
  11. use rustc_ast as ast;
  12. use rustc_hir::diagnostic_items::DiagnosticItems;
  13. use rustc_middle::ty::query::Providers;
  14. use rustc_middle::ty::TyCtxt;
  15. use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
  16. use rustc_span::symbol::{sym, Symbol};
  17. fn observe_item<'tcx>(
  18. tcx: TyCtxt<'tcx>,
  19. diagnostic_items: &mut DiagnosticItems,
  20. def_id: LocalDefId,
  21. ) {
  22. let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
  23. let attrs = tcx.hir().attrs(hir_id);
  24. if let Some(name) = extract(attrs) {
  25. // insert into our table
  26. collect_item(tcx, diagnostic_items, name, def_id.to_def_id());
  27. }
  28. }
  29. fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item_def_id: DefId) {
  30. items.id_to_name.insert(item_def_id, name);
  31. if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
  32. if original_def_id != item_def_id {
  33. let mut err = match tcx.hir().span_if_local(item_def_id) {
  34. Some(span) => tcx
  35. .sess
  36. .struct_span_err(span, &format!("duplicate diagnostic item found: `{name}`.")),
  37. None => tcx.sess.struct_err(&format!(
  38. "duplicate diagnostic item in crate `{}`: `{}`.",
  39. tcx.crate_name(item_def_id.krate),
  40. name
  41. )),
  42. };
  43. if let Some(span) = tcx.hir().span_if_local(original_def_id) {
  44. err.span_note(span, "the diagnostic item is first defined here");
  45. } else {
  46. err.note(&format!(
  47. "the diagnostic item is first defined in crate `{}`.",
  48. tcx.crate_name(original_def_id.krate)
  49. ));
  50. }
  51. err.emit();
  52. }
  53. }
  54. }
  55. /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
  56. fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
  57. attrs.iter().find_map(|attr| {
  58. if attr.has_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
  59. })
  60. }
  61. /// Traverse and collect the diagnostic items in the current
  62. fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems {
  63. assert_eq!(cnum, LOCAL_CRATE);
  64. // Initialize the collector.
  65. let mut diagnostic_items = DiagnosticItems::default();
  66. // Collect diagnostic items in this crate.
  67. let crate_items = tcx.hir_crate_items(());
  68. for id in crate_items.items() {
  69. observe_item(tcx, &mut diagnostic_items, id.def_id);
  70. }
  71. for id in crate_items.trait_items() {
  72. observe_item(tcx, &mut diagnostic_items, id.def_id);
  73. }
  74. for id in crate_items.impl_items() {
  75. observe_item(tcx, &mut diagnostic_items, id.def_id);
  76. }
  77. for id in crate_items.foreign_items() {
  78. observe_item(tcx, &mut diagnostic_items, id.def_id);
  79. }
  80. diagnostic_items
  81. }
  82. /// Traverse and collect all the diagnostic items in all crates.
  83. fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> DiagnosticItems {
  84. // Initialize the collector.
  85. let mut items = DiagnosticItems::default();
  86. // Collect diagnostic items in other crates.
  87. for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
  88. for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
  89. collect_item(tcx, &mut items, name, def_id);
  90. }
  91. }
  92. items
  93. }
  94. pub fn provide(providers: &mut Providers) {
  95. providers.diagnostic_items = diagnostic_items;
  96. providers.all_diagnostic_items = all_diagnostic_items;
  97. }