From 36a28060f146a5c5ff6445659ce5962009c8829d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 5 Feb 2021 15:47:44 +0100 Subject: [PATCH 1/6] Merge the BTreeMap in hir::Crate. --- compiler/rustc_ast_lowering/src/item.rs | 16 +- compiler/rustc_ast_lowering/src/lib.rs | 59 ++-- compiler/rustc_hir/src/arena.rs | 3 + compiler/rustc_hir/src/hir.rs | 252 ++++++++++++++---- compiler/rustc_hir/src/intravisit.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_lint/src/levels.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../rustc_middle/src/hir/map/collector.rs | 8 +- compiler/rustc_middle/src/hir/map/mod.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- compiler/rustc_passes/src/diagnostic_items.rs | 2 +- compiler/rustc_resolve/src/late/lifetimes.rs | 29 +- src/librustdoc/visit_ast.rs | 2 +- 14 files changed, 268 insertions(+), 119 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 9f9d41c3f3d9e..852f696ef1400 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -82,15 +82,11 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt { AssocCtxt::Trait => { let hir_item = lctx.lower_trait_item(item); - let id = hir_item.trait_item_id(); - lctx.trait_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().trait_items.insert(id); + lctx.insert_trait_item(hir_item); } AssocCtxt::Impl => { let hir_item = lctx.lower_impl_item(item); - let id = hir_item.impl_item_id(); - lctx.impl_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().impl_items.insert(id); + lctx.insert_impl_item(hir_item); } }); @@ -101,9 +97,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { self.lctx.allocate_hir_id_counter(item.id); self.lctx.with_hir_id_owner(item.id, |lctx| { let hir_item = lctx.lower_foreign_item(item); - let id = hir_item.foreign_item_id(); - lctx.foreign_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().foreign_items.insert(id); + lctx.insert_foreign_item(hir_item); }); visit::walk_foreign_item(self, item); @@ -123,7 +117,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> T { let old_len = self.in_scope_lifetimes.len(); - let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind { + let parent_generics = match self.owners[parent_hir_id.def_id].unwrap().expect_item().kind { hir::ItemKind::Impl(hir::Impl { ref generics, .. }) | hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params, _ => &[], @@ -224,7 +218,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let hir_id = self.lower_node_id(i.id); self.lower_attrs(hir_id, &i.attrs); let body = P(self.lower_mac_args(body)); - self.exported_macros.push(hir::MacroDef { + self.insert_macro_def(hir::MacroDef { ident, vis, def_id: hir_id.expect_owner(), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d4caba9241600..540abe6e48ef9 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -101,13 +101,8 @@ struct LoweringContext<'a, 'hir: 'a> { arena: &'hir Arena<'hir>, /// The items being lowered are collected here. - items: BTreeMap>, - - trait_items: BTreeMap>, - impl_items: BTreeMap>, - foreign_items: BTreeMap>, + owners: IndexVec>>, bodies: BTreeMap>, - exported_macros: Vec>, non_exported_macro_attrs: Vec, trait_impls: BTreeMap>, @@ -330,15 +325,11 @@ pub fn lower_crate<'a, 'hir>( resolver, nt_to_tokenstream, arena, - items: BTreeMap::new(), - trait_items: BTreeMap::new(), - impl_items: BTreeMap::new(), - foreign_items: BTreeMap::new(), + owners: IndexVec::default(), bodies: BTreeMap::new(), trait_impls: BTreeMap::new(), modules: BTreeMap::new(), attrs: BTreeMap::default(), - exported_macros: Vec::new(), non_exported_macro_attrs: Vec::new(), catch_scopes: Vec::new(), loop_scopes: Vec::new(), @@ -558,12 +549,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let krate = hir::Crate { item: module, - exported_macros: self.arena.alloc_from_iter(self.exported_macros), non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs), - items: self.items, - trait_items: self.trait_items, - impl_items: self.impl_items, - foreign_items: self.foreign_items, + owners: self.owners, bodies: self.bodies, body_ids, trait_impls: self.trait_impls, @@ -576,12 +563,48 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId { - let id = hir::ItemId { def_id: item.def_id }; - self.items.insert(id, item); + let id = item.item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::Item(item)); self.modules.entry(self.current_module).or_default().items.insert(id); id } + fn insert_foreign_item(&mut self, item: hir::ForeignItem<'hir>) -> hir::ForeignItemId { + let id = item.foreign_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::ForeignItem(item)); + self.modules.entry(self.current_module).or_default().foreign_items.insert(id); + id + } + + fn insert_impl_item(&mut self, item: hir::ImplItem<'hir>) -> hir::ImplItemId { + let id = item.impl_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::ImplItem(item)); + self.modules.entry(self.current_module).or_default().impl_items.insert(id); + id + } + + fn insert_trait_item(&mut self, item: hir::TraitItem<'hir>) -> hir::TraitItemId { + let id = item.trait_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::TraitItem(item)); + self.modules.entry(self.current_module).or_default().trait_items.insert(id); + id + } + + fn insert_macro_def(&mut self, item: hir::MacroDef<'hir>) { + let def_id = item.def_id; + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(def_id, || None); + self.owners[def_id] = Some(hir::OwnerNode::MacroDef(item)); + } + fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId { // Set up the counter if needed. self.item_local_id_counters.entry(owner).or_insert(0); diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index b05ca381b8ab6..4e8cf009495eb 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -29,7 +29,9 @@ macro_rules! arena_types { [] fn_decl: rustc_hir::FnDecl<$tcx>, [] foreign_item: rustc_hir::ForeignItem<$tcx>, [few] foreign_item_ref: rustc_hir::ForeignItemRef<$tcx>, + [] impl_item: rustc_hir::ImplItem<$tcx>, [] impl_item_ref: rustc_hir::ImplItemRef<$tcx>, + [] item: rustc_hir::Item<$tcx>, [few] inline_asm: rustc_hir::InlineAsm<$tcx>, [few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>, [] local: rustc_hir::Local<$tcx>, @@ -42,6 +44,7 @@ macro_rules! arena_types { [] qpath: rustc_hir::QPath<$tcx>, [] stmt: rustc_hir::Stmt<$tcx>, [] field_def: rustc_hir::FieldDef<$tcx>, + [] trait_item: rustc_hir::TraitItem<$tcx>, [] trait_item_ref: rustc_hir::TraitItemRef, [] ty: rustc_hir::Ty<$tcx>, [] type_binding: rustc_hir::TypeBinding<$tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 6aff2fdbd1f22..f9b83f88ee4b9 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -12,6 +12,7 @@ pub use rustc_ast::{CaptureBy, Movability, Mutability}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; +use rustc_index::vec::IndexVec; use rustc_macros::HashStable_Generic; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -628,21 +629,10 @@ pub struct ModuleItems { #[derive(Debug)] pub struct Crate<'hir> { pub item: Mod<'hir>, - pub exported_macros: &'hir [MacroDef<'hir>], // Attributes from non-exported macros, kept only for collecting the library feature list. pub non_exported_macro_attrs: &'hir [Attribute], - // N.B., we use a `BTreeMap` here so that `visit_all_items` iterates - // over the ids in increasing order. In principle it should not - // matter what order we visit things in, but in *practice* it - // does, because it can affect the order in which errors are - // detected, which in turn can make UI tests yield - // slightly different results. - pub items: BTreeMap>, - - pub trait_items: BTreeMap>, - pub impl_items: BTreeMap>, - pub foreign_items: BTreeMap>, + pub owners: IndexVec>>, pub bodies: BTreeMap>, pub trait_impls: BTreeMap>, @@ -668,20 +658,20 @@ pub struct Crate<'hir> { } impl Crate<'hir> { - pub fn item(&self, id: ItemId) -> &Item<'hir> { - &self.items[&id] + pub fn item(&self, id: ItemId) -> &'hir Item<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_item() } - pub fn trait_item(&self, id: TraitItemId) -> &TraitItem<'hir> { - &self.trait_items[&id] + pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_trait_item() } - pub fn impl_item(&self, id: ImplItemId) -> &ImplItem<'hir> { - &self.impl_items[&id] + pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_impl_item() } - pub fn foreign_item(&self, id: ForeignItemId) -> &ForeignItem<'hir> { - &self.foreign_items[&id] + pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_foreign_item() } pub fn body(&self, id: BodyId) -> &Body<'hir> { @@ -702,20 +692,14 @@ impl Crate<'_> { where V: itemlikevisit::ItemLikeVisitor<'hir>, { - for item in self.items.values() { - visitor.visit_item(item); - } - - for trait_item in self.trait_items.values() { - visitor.visit_trait_item(trait_item); - } - - for impl_item in self.impl_items.values() { - visitor.visit_impl_item(impl_item); - } - - for foreign_item in self.foreign_items.values() { - visitor.visit_foreign_item(foreign_item); + for owner in self.owners.iter().filter_map(Option::as_ref) { + match owner { + OwnerNode::Item(item) => visitor.visit_item(item), + OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item), + OwnerNode::ImplItem(item) => visitor.visit_impl_item(item), + OwnerNode::TraitItem(item) => visitor.visit_trait_item(item), + OwnerNode::MacroDef(_) => {} + } } } @@ -724,28 +708,27 @@ impl Crate<'_> { where V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send, { - parallel!( - { - par_for_each_in(&self.items, |(_, item)| { - visitor.visit_item(item); - }); - }, - { - par_for_each_in(&self.trait_items, |(_, trait_item)| { - visitor.visit_trait_item(trait_item); - }); - }, - { - par_for_each_in(&self.impl_items, |(_, impl_item)| { - visitor.visit_impl_item(impl_item); - }); - }, - { - par_for_each_in(&self.foreign_items, |(_, foreign_item)| { - visitor.visit_foreign_item(foreign_item); - }); - } - ); + par_for_each_in(&self.owners.raw, |owner| match owner { + Some(OwnerNode::Item(item)) => visitor.visit_item(item), + Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item), + Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item), + Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item), + Some(OwnerNode::MacroDef(_)) | None => {} + }) + } + + pub fn items<'hir>(&'hir self) -> impl Iterator> + 'hir { + self.owners.iter().filter_map(|owner| match owner { + Some(OwnerNode::Item(item)) => Some(*item), + _ => None, + }) + } + + pub fn exported_macros<'hir>(&'hir self) -> impl Iterator> + 'hir { + self.owners.iter().filter_map(|owner| match owner { + Some(OwnerNode::MacroDef(macro_def)) => Some(*macro_def), + _ => None, + }) } } @@ -2953,6 +2936,163 @@ pub struct TraitCandidate { pub import_ids: SmallVec<[LocalDefId; 1]>, } +#[derive(Copy, Clone, Debug, HashStable_Generic)] +pub enum OwnerNode<'hir> { + Item(&'hir Item<'hir>), + ForeignItem(&'hir ForeignItem<'hir>), + TraitItem(&'hir TraitItem<'hir>), + ImplItem(&'hir ImplItem<'hir>), + MacroDef(&'hir MacroDef<'hir>), +} + +impl<'hir> OwnerNode<'hir> { + pub fn ident(&self) -> Ident { + match self { + OwnerNode::Item(Item { ident, .. }) + | OwnerNode::ForeignItem(ForeignItem { ident, .. }) + | OwnerNode::ImplItem(ImplItem { ident, .. }) + | OwnerNode::TraitItem(TraitItem { ident, .. }) + | OwnerNode::MacroDef(MacroDef { ident, .. }) => *ident, + } + } + + pub fn fn_decl(&self) -> Option<&FnDecl<'hir>> { + match self { + OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. }) + | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. }) + | OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl), + OwnerNode::ForeignItem(ForeignItem { + kind: ForeignItemKind::Fn(fn_decl, _, _), + .. + }) => Some(fn_decl), + _ => None, + } + } + + pub fn body_id(&self) -> Option { + match self { + OwnerNode::TraitItem(TraitItem { + kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)), + .. + }) + | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. }) + | OwnerNode::Item(Item { kind: ItemKind::Fn(.., body_id), .. }) => Some(*body_id), + _ => None, + } + } + + pub fn generics(&self) -> Option<&'hir Generics<'hir>> { + match self { + OwnerNode::TraitItem(TraitItem { generics, .. }) + | OwnerNode::ImplItem(ImplItem { generics, .. }) => Some(generics), + OwnerNode::Item(item) => item.kind.generics(), + _ => None, + } + } + + pub fn def_id(self) -> LocalDefId { + match self { + OwnerNode::Item(Item { def_id, .. }) + | OwnerNode::TraitItem(TraitItem { def_id, .. }) + | OwnerNode::ImplItem(ImplItem { def_id, .. }) + | OwnerNode::ForeignItem(ForeignItem { def_id, .. }) + | OwnerNode::MacroDef(MacroDef { def_id, .. }) => *def_id, + } + } + + pub fn expect_item(self) -> &'hir Item<'hir> { + match self { + OwnerNode::Item(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_foreign_item(self) -> &'hir ForeignItem<'hir> { + match self { + OwnerNode::ForeignItem(n) => n, + OwnerNode::Item(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_impl_item(self) -> &'hir ImplItem<'hir> { + match self { + OwnerNode::ImplItem(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::Item(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_trait_item(self) -> &'hir TraitItem<'hir> { + match self { + OwnerNode::TraitItem(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::Item(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_macro_def(self) -> &'hir MacroDef<'hir> { + match self { + OwnerNode::MacroDef(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::Item(_) => panic!(), + } + } +} + +impl<'hir> Into> for &'hir Item<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::Item(self) + } +} + +impl<'hir> Into> for &'hir ForeignItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::ForeignItem(self) + } +} + +impl<'hir> Into> for &'hir ImplItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::ImplItem(self) + } +} + +impl<'hir> Into> for &'hir TraitItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::TraitItem(self) + } +} + +impl<'hir> Into> for &'hir MacroDef<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::MacroDef(self) + } +} + +impl<'hir> Into> for OwnerNode<'hir> { + fn into(self) -> Node<'hir> { + match self { + OwnerNode::Item(n) => Node::Item(n), + OwnerNode::ForeignItem(n) => Node::ForeignItem(n), + OwnerNode::ImplItem(n) => Node::ImplItem(n), + OwnerNode::TraitItem(n) => Node::TraitItem(n), + OwnerNode::MacroDef(n) => Node::MacroDef(n), + } + } +} + #[derive(Copy, Clone, Debug, HashStable_Generic)] pub enum Node<'hir> { Param(&'hir Param<'hir>), diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index c08f1f53218d6..83a4b2370d076 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -479,7 +479,7 @@ pub trait Visitor<'v>: Sized { /// Walks the contents of a crate. See also `Crate::visit_all_items`. pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) { visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID); - walk_list!(visitor, visit_macro_def, krate.exported_macros); + walk_list!(visitor, visit_macro_def, krate.exported_macros()); for (&id, attrs) in krate.attrs.iter() { for a in *attrs { visitor.visit_attribute(id, a) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ccdbccae156c3..7d87e708eae52 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -570,7 +570,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) { self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.inner, "the", "crate"); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { // Non exported macros should be skipped, since `missing_docs` only // applies to externally visible items. if !cx.access_levels.is_exported(macro_def.hir_id()) { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 069fa41fa886a..4f223afcc27a0 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -33,11 +33,11 @@ fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { let mut builder = LintLevelMapBuilder { levels, tcx, store }; let krate = tcx.hir().krate(); - builder.levels.id_to_set.reserve(krate.exported_macros.len() + 1); + builder.levels.id_to_set.reserve(krate.owners.len() + 1); let push = builder.levels.push(tcx.hir().attrs(hir::CRATE_HIR_ID), &store, true); builder.levels.register_id(hir::CRATE_HIR_ID); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { builder.levels.register_id(macro_def.hir_id()); } intravisit::walk_crate(&mut builder, krate); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a4913a32e81ef..e40c885fce90c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -448,7 +448,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } krate.visit_all_item_likes(&mut self.as_deep_visitor()); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { self.visit_macro_def(macro_def); } } diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs index 8ffd98326f1c8..be8863be62733 100644 --- a/compiler/rustc_middle/src/hir/map/collector.rs +++ b/compiler/rustc_middle/src/hir/map/collector.rs @@ -81,14 +81,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { let Crate { ref item, // These fields are handled separately: - exported_macros: _, non_exported_macro_attrs: _, - items: _, - trait_items: _, - impl_items: _, - foreign_items: _, - bodies: _, + owners: _, trait_impls: _, + bodies: _, body_ids: _, modules: _, proc_macros: _, diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 392372fad531e..edcbce6b09881 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -554,8 +554,8 @@ impl<'hir> Map<'hir> { where V: Visitor<'hir>, { - for id in self.krate().exported_macros { - visitor.visit_macro_def(self.expect_macro_def(id.hir_id())); + for macro_def in self.krate().exported_macros() { + visitor.visit_macro_def(macro_def); } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index b5733bd2edc2e..dc94124e62ab6 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2307,7 +2307,7 @@ define_print_and_forward_display! { fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) { // Iterate all local crate items no matter where they are defined. let hir = tcx.hir(); - for item in hir.krate().items.values() { + for item in hir.krate().items() { if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) { continue; } diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index ddcc6fc123f7b..3cf1d0cdd94b7 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -108,7 +108,7 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); - let parent_is_item = if let Some(parent_def_id) = - parent_id.as_owner() - { - let parent_item_id = hir::ItemId { def_id: parent_def_id }; - let parent_impl_id = hir::ImplItemId { def_id: parent_def_id }; - let parent_trait_id = - hir::TraitItemId { def_id: parent_def_id }; - let parent_foreign_id = - hir::ForeignItemId { def_id: parent_def_id }; - let krate = self.tcx.hir().krate(); - - krate.items.contains_key(&parent_item_id) - || krate.impl_items.contains_key(&parent_impl_id) - || krate.trait_items.contains_key(&parent_trait_id) - || krate.foreign_items.contains_key(&parent_foreign_id) - } else { - false - }; + // FIXME(cjgillot) Can this check be replaced by + // `let parent_is_item = parent_id.is_owner();`? + let parent_is_item = + if let Some(parent_def_id) = parent_id.as_owner() { + matches!( + self.tcx.hir().krate().owners.get(parent_def_id), + Some(Some(_)), + ) + } else { + false + }; if !parent_is_item { if !self.trait_definition_only { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index b563c4f479935..3621a6cb7d81a 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -83,7 +83,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as // well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by // moving them back to their correct locations. - 'exported_macros: for def in krate.exported_macros { + 'exported_macros: for def in krate.exported_macros() { // The `def` of a macro in `exported_macros` should correspond to either: // - a `#[macro_export] macro_rules!` macro, // - a built-in `derive` (or attribute) macro such as the ones in `::core`, From fee421685d9b29a7a865a13dc1c5a76816bcf417 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 25 Jul 2021 12:03:24 +0200 Subject: [PATCH 2/6] Introduce OwnerNode::Crate. --- compiler/rustc_ast_lowering/src/lib.rs | 6 +- compiler/rustc_hir/src/arena.rs | 1 + compiler/rustc_hir/src/hir.rs | 67 ++++++++++++------- compiler/rustc_hir/src/intravisit.rs | 3 +- compiler/rustc_hir_pretty/src/lib.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../rustc_middle/src/hir/map/collector.rs | 20 +----- compiler/rustc_passes/src/entry.rs | 2 +- compiler/rustc_passes/src/stability.rs | 4 +- .../rustc_save_analysis/src/dump_visitor.rs | 9 +-- src/librustdoc/clean/types.rs | 4 +- src/librustdoc/doctest.rs | 2 +- src/librustdoc/visit_ast.rs | 4 +- .../auxiliary/lint-for-crate-rpass.rs | 2 +- .../ui-fulldeps/auxiliary/lint-for-crate.rs | 2 +- .../clippy/clippy_lints/src/missing_doc.rs | 2 +- 17 files changed, 69 insertions(+), 65 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 540abe6e48ef9..57bf7be40566e 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -512,8 +512,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c); visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c); - let module = self.lower_mod(&c.items, c.span); + let module = self.arena.alloc(self.lower_mod(&c.items, c.span)); self.lower_attrs(hir::CRATE_HIR_ID, &c.attrs); + self.owners.ensure_contains_elem(CRATE_DEF_ID, || None); + self.owners[CRATE_DEF_ID] = Some(hir::OwnerNode::Crate(module)); + let body_ids = body_ids(&self.bodies); let proc_macros = c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect(); @@ -548,7 +551,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } let krate = hir::Crate { - item: module, non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs), owners: self.owners, bodies: self.bodies, diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 4e8cf009495eb..c4cff79f6c525 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -36,6 +36,7 @@ macro_rules! arena_types { [few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>, [] local: rustc_hir::Local<$tcx>, [few] macro_def: rustc_hir::MacroDef<$tcx>, + [few] mod_: rustc_hir::Mod<$tcx>, [] param: rustc_hir::Param<$tcx>, [] pat: rustc_hir::Pat<$tcx>, [] path: rustc_hir::Path<$tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index f9b83f88ee4b9..23b28733e2ab1 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,6 +1,6 @@ // ignore-tidy-filelength use crate::def::{CtorKind, DefKind, Res}; -use crate::def_id::DefId; +use crate::def_id::{DefId, CRATE_DEF_ID}; crate use crate::hir_id::{HirId, ItemLocalId}; use crate::{itemlikevisit, LangItem}; @@ -628,7 +628,6 @@ pub struct ModuleItems { /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html #[derive(Debug)] pub struct Crate<'hir> { - pub item: Mod<'hir>, // Attributes from non-exported macros, kept only for collecting the library feature list. pub non_exported_macro_attrs: &'hir [Attribute], @@ -658,6 +657,10 @@ pub struct Crate<'hir> { } impl Crate<'hir> { + pub fn module(&self) -> &'hir Mod<'hir> { + if let Some(OwnerNode::Crate(m)) = self.owners[CRATE_DEF_ID] { m } else { panic!() } + } + pub fn item(&self, id: ItemId) -> &'hir Item<'hir> { self.owners[id.def_id].as_ref().unwrap().expect_item() } @@ -698,7 +701,7 @@ impl Crate<'_> { OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item), OwnerNode::ImplItem(item) => visitor.visit_impl_item(item), OwnerNode::TraitItem(item) => visitor.visit_trait_item(item), - OwnerNode::MacroDef(_) => {} + OwnerNode::MacroDef(_) | OwnerNode::Crate(_) => {} } } } @@ -713,7 +716,7 @@ impl Crate<'_> { Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item), Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item), Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item), - Some(OwnerNode::MacroDef(_)) | None => {} + Some(OwnerNode::MacroDef(_)) | Some(OwnerNode::Crate(_)) | None => {} }) } @@ -2943,16 +2946,29 @@ pub enum OwnerNode<'hir> { TraitItem(&'hir TraitItem<'hir>), ImplItem(&'hir ImplItem<'hir>), MacroDef(&'hir MacroDef<'hir>), + Crate(&'hir Mod<'hir>), } impl<'hir> OwnerNode<'hir> { - pub fn ident(&self) -> Ident { + pub fn ident(&self) -> Option { match self { OwnerNode::Item(Item { ident, .. }) | OwnerNode::ForeignItem(ForeignItem { ident, .. }) | OwnerNode::ImplItem(ImplItem { ident, .. }) | OwnerNode::TraitItem(TraitItem { ident, .. }) - | OwnerNode::MacroDef(MacroDef { ident, .. }) => *ident, + | OwnerNode::MacroDef(MacroDef { ident, .. }) => Some(*ident), + OwnerNode::Crate(..) => None, + } + } + + pub fn span(&self) -> Span { + match self { + OwnerNode::Item(Item { span, .. }) + | OwnerNode::ForeignItem(ForeignItem { span, .. }) + | OwnerNode::ImplItem(ImplItem { span, .. }) + | OwnerNode::TraitItem(TraitItem { span, .. }) + | OwnerNode::MacroDef(MacroDef { span, .. }) + | OwnerNode::Crate(Mod { inner: span, .. }) => *span, } } @@ -2997,56 +3013,42 @@ impl<'hir> OwnerNode<'hir> { | OwnerNode::ImplItem(ImplItem { def_id, .. }) | OwnerNode::ForeignItem(ForeignItem { def_id, .. }) | OwnerNode::MacroDef(MacroDef { def_id, .. }) => *def_id, + OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner, } } pub fn expect_item(self) -> &'hir Item<'hir> { match self { OwnerNode::Item(n) => n, - OwnerNode::ForeignItem(_) - | OwnerNode::ImplItem(_) - | OwnerNode::TraitItem(_) - | OwnerNode::MacroDef(_) => panic!(), + _ => panic!(), } } pub fn expect_foreign_item(self) -> &'hir ForeignItem<'hir> { match self { OwnerNode::ForeignItem(n) => n, - OwnerNode::Item(_) - | OwnerNode::ImplItem(_) - | OwnerNode::TraitItem(_) - | OwnerNode::MacroDef(_) => panic!(), + _ => panic!(), } } pub fn expect_impl_item(self) -> &'hir ImplItem<'hir> { match self { OwnerNode::ImplItem(n) => n, - OwnerNode::ForeignItem(_) - | OwnerNode::Item(_) - | OwnerNode::TraitItem(_) - | OwnerNode::MacroDef(_) => panic!(), + _ => panic!(), } } pub fn expect_trait_item(self) -> &'hir TraitItem<'hir> { match self { OwnerNode::TraitItem(n) => n, - OwnerNode::ForeignItem(_) - | OwnerNode::ImplItem(_) - | OwnerNode::Item(_) - | OwnerNode::MacroDef(_) => panic!(), + _ => panic!(), } } pub fn expect_macro_def(self) -> &'hir MacroDef<'hir> { match self { OwnerNode::MacroDef(n) => n, - OwnerNode::ForeignItem(_) - | OwnerNode::ImplItem(_) - | OwnerNode::TraitItem(_) - | OwnerNode::Item(_) => panic!(), + _ => panic!(), } } } @@ -3089,6 +3091,7 @@ impl<'hir> Into> for OwnerNode<'hir> { OwnerNode::ImplItem(n) => Node::ImplItem(n), OwnerNode::TraitItem(n) => Node::TraitItem(n), OwnerNode::MacroDef(n) => Node::MacroDef(n), + OwnerNode::Crate(n) => Node::Crate(n), } } } @@ -3221,6 +3224,18 @@ impl<'hir> Node<'hir> { _ => Constness::NotConst, } } + + pub fn as_owner(self) -> Option> { + match self { + Node::Item(i) => Some(OwnerNode::Item(i)), + Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)), + Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)), + Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)), + Node::MacroDef(i) => Some(OwnerNode::MacroDef(i)), + Node::Crate(i) => Some(OwnerNode::Crate(i)), + _ => None, + } + } } // Some nodes are used a lot. Make sure they don't unintentionally get bigger. diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 83a4b2370d076..711b62c4a3142 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -478,7 +478,8 @@ pub trait Visitor<'v>: Sized { /// Walks the contents of a crate. See also `Crate::visit_all_items`. pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) { - visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID); + let top_mod = krate.module(); + visitor.visit_mod(top_mod, top_mod.inner, CRATE_HIR_ID); walk_list!(visitor, visit_macro_def, krate.exported_macros()); for (&id, attrs) in krate.attrs.iter() { for a in *attrs { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 5c1739b1ab9b6..4177c2f8d525e 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -169,7 +169,7 @@ pub fn print_crate<'a>( // When printing the AST, we sometimes need to inject `#[no_std]` here. // Since you can't compile the HIR, it's not necessary. - s.print_mod(&krate.item, s.attrs(hir::CRATE_HIR_ID)); + s.print_mod(&krate.module(), s.attrs(hir::CRATE_HIR_ID)); s.print_remaining_comments(); s.s.eof() } diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 7d87e708eae52..b3c64b76820f1 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -568,7 +568,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { } fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) { - self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.inner, "the", "crate"); + self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.module().inner, "the", "crate"); for macro_def in krate.exported_macros() { // Non exported macros should be skipped, since `missing_docs` only diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e40c885fce90c..45a4762c700e4 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -439,7 +439,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_items(&mut self) { let krate = self.tcx.hir().krate(); - self.encode_info_for_mod(CRATE_DEF_ID, &krate.item); + self.encode_info_for_mod(CRATE_DEF_ID, krate.module()); // Proc-macro crates only export proc-macro items, which are looked // up using `proc_macro_data` diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs index be8863be62733..09060169c5f4b 100644 --- a/compiler/rustc_middle/src/hir/map/collector.rs +++ b/compiler/rustc_middle/src/hir/map/collector.rs @@ -77,23 +77,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { definitions: &'a definitions::Definitions, mut hcx: StableHashingContext<'a>, ) -> NodeCollector<'a, 'hir> { - let hash = { - let Crate { - ref item, - // These fields are handled separately: - non_exported_macro_attrs: _, - owners: _, - trait_impls: _, - bodies: _, - body_ids: _, - modules: _, - proc_macros: _, - trait_map: _, - attrs: _, - } = *krate; - - hash_body(&mut hcx, item) - }; + let hash = hash_body(&mut hcx, krate.module()); let mut collector = NodeCollector { arena, @@ -108,7 +92,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { }; collector.insert_entry( hir::CRATE_HIR_ID, - Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) }, + Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.module()) }, hash, ); diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index 550f4f148fd24..876edbd1f6d55 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -183,7 +183,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De } fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { - let sp = tcx.hir().krate().item.inner; + let sp = tcx.hir().krate().module().inner; if *tcx.sess.parse_sess.reached_eof.borrow() { // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about // the missing `fn main()` then as it might have been hidden inside an unclosed block. diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 42a4753c29c9b..3db0409d8f009 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -732,7 +732,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> { annotator.annotate( hir::CRATE_HIR_ID, - krate.item.inner, + krate.module().inner, None, AnnotationKind::Required, InheritDeprecation::Yes, @@ -929,7 +929,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) { if tcx.stability().staged_api[&LOCAL_CRATE] { let krate = tcx.hir().krate(); let mut missing = MissingStabilityAnnotations { tcx, access_levels }; - missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.inner); + missing.check_missing_stability(hir::CRATE_HIR_ID, krate.module().inner); intravisit::walk_crate(&mut missing, krate); krate.visit_all_item_likes(&mut missing.as_deep_visitor()); } diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index 842f7f9deee38..4f8dc7d16d4b9 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -146,7 +146,7 @@ impl<'tcx> DumpVisitor<'tcx> { }, crate_root: crate_root.unwrap_or_else(|| "".to_owned()), external_crates: self.save_ctxt.get_external_crates(), - span: self.span_from_span(krate.item.inner), + span: self.span_from_span(krate.module().inner), }; self.dumper.crate_prelude(data); @@ -1092,11 +1092,12 @@ impl<'tcx> DumpVisitor<'tcx> { format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id())); let sm = self.tcx.sess.source_map(); - let filename = sm.span_to_filename(krate.item.inner); + let krate_mod = krate.module(); + let filename = sm.span_to_filename(krate_mod.inner); let data_id = id_from_hir_id(id, &self.save_ctxt); let children = - krate.item.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect(); - let span = self.span_from_span(krate.item.inner); + krate_mod.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect(); + let span = self.span_from_span(krate_mod.inner); let attrs = self.tcx.hir().attrs(id); self.dumper.dump_def( diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 2fd2d14bcabc3..101d3b400c3df 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -227,7 +227,7 @@ impl ExternalCrate { if root.is_local() { tcx.hir() .krate() - .item + .module() .item_ids .iter() .filter_map(|&id| { @@ -293,7 +293,7 @@ impl ExternalCrate { if root.is_local() { tcx.hir() .krate() - .item + .module() .item_ids .iter() .filter_map(|&id| { diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index b45e84aff8cf5..d5268abeec795 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -144,7 +144,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> { hir_collector.visit_testable( "".to_string(), CRATE_HIR_ID, - krate.item.inner, + krate.module().inner, |this| { intravisit::walk_crate(this, krate); }, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 3621a6cb7d81a..d74b3b4627284 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -72,11 +72,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> { - let span = krate.item.inner; + let span = krate.module().inner; let mut top_level_module = self.visit_mod_contents( &Spanned { span, node: hir::VisibilityKind::Public }, hir::CRATE_HIR_ID, - &krate.item, + &krate.module(), self.cx.tcx.crate_name(LOCAL_CRATE), ); // Attach the crate's exported macros to the top-level module. diff --git a/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs b/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs index 51cea4f6ba91b..1f494e444846f 100644 --- a/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs +++ b/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs @@ -33,7 +33,7 @@ macro_rules! fake_lint_pass { if !cx.sess().contains_name(attrs, $attr) { cx.lint(CRATE_NOT_OKAY, |lint| { let msg = format!("crate is not marked with #![{}]", $attr); - lint.build(&msg).set_span(krate.item.inner).emit() + lint.build(&msg).set_span(krate.module().inner).emit() }); } )* diff --git a/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs b/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs index ef5353e6d8cf1..122a544e9d469 100644 --- a/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs +++ b/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs @@ -31,7 +31,7 @@ impl<'tcx> LateLintPass<'tcx> for Pass { if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) { cx.lint(CRATE_NOT_OKAY, |lint| { lint.build("crate is not marked with #![crate_okay]") - .set_span(krate.item.inner) + .set_span(krate.module().inner) .emit() }); } diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index a46a7407df0ce..6ad702f8eafdf 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -106,7 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) { let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID); - self.check_missing_docs_attrs(cx, attrs, krate.item.inner, "the", "crate"); + self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate"); } fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) { From b88083a58c557d7f7f2289333f45944785465cf1 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 27 Mar 2021 13:21:26 +0100 Subject: [PATCH 3/6] Use OwnerNode in indexing. --- .../rustc_middle/src/hir/map/collector.rs | 153 ++++++------------ compiler/rustc_middle/src/hir/map/mod.rs | 2 +- compiler/rustc_middle/src/hir/mod.rs | 8 +- 3 files changed, 57 insertions(+), 106 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs index 09060169c5f4b..0eb0c3eca4e3f 100644 --- a/compiler/rustc_middle/src/hir/map/collector.rs +++ b/compiler/rustc_middle/src/hir/map/collector.rs @@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; -use rustc_hir::def_id::CRATE_DEF_INDEX; +use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::definitions; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::*; @@ -75,26 +75,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { arena: &'hir Arena<'hir>, krate: &'hir Crate<'hir>, definitions: &'a definitions::Definitions, - mut hcx: StableHashingContext<'a>, + hcx: StableHashingContext<'a>, ) -> NodeCollector<'a, 'hir> { - let hash = hash_body(&mut hcx, krate.module()); - let mut collector = NodeCollector { arena, krate, source_map: sess.source_map(), parent_node: hir::CRATE_HIR_ID, - current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX }, + current_dep_node_owner: CRATE_DEF_ID, definitions, hcx, map: IndexVec::from_fn_n(|_| None, definitions.def_index_count()), parenting: FxHashMap::default(), }; - collector.insert_entry( - hir::CRATE_HIR_ID, - Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.module()) }, - hash, - ); + collector.insert_owner(CRATE_DEF_ID, OwnerNode::Crate(krate.module())); collector } @@ -108,53 +102,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { IndexedHir { map: self.map, parenting: self.parenting } } - fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) { - let i = id.local_id.as_u32() as usize; - - let arena = self.arena; - - let data = &mut self.map[id.owner]; - - if i == 0 { - debug_assert!(data.is_none()); - *data = Some(arena.alloc(OwnerNodes { - hash, - nodes: IndexVec::new(), - bodies: FxHashMap::default(), - })); - - let dk_parent = self.definitions.def_key(id.owner).parent; - if let Some(dk_parent) = dk_parent { - let dk_parent = LocalDefId { local_def_index: dk_parent }; - let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent); - if dk_parent.owner != entry.parent.owner { - panic!( - "Different parents for {:?} => dk_parent={:?} actual={:?}", - id.owner, dk_parent, entry.parent, - ) - } - - debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent)); - } - } else { - debug_assert_eq!(entry.parent.owner, id.owner); - } + fn insert_owner(&mut self, owner: LocalDefId, node: OwnerNode<'hir>) { + let hash = hash_body(&mut self.hcx, node); - let data = data.as_mut().unwrap(); + let mut nodes = IndexVec::new(); + nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: node.into() })); - insert_vec_map( - &mut data.nodes, - id.local_id, - ParentedNode { parent: entry.parent.local_id, node: entry.node }, - ); + debug_assert!(self.map[owner].is_none()); + self.map[owner] = + Some(self.arena.alloc(OwnerNodes { hash, nodes, bodies: FxHashMap::default() })); } fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) { - self.insert_with_hash(span, hir_id, node, Fingerprint::ZERO) - } - - fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) { - let entry = Entry { parent: self.parent_node, node }; + debug_assert_eq!(self.current_dep_node_owner, hir_id.owner); + debug_assert_ne!(hir_id.local_id.as_u32(), 0); // Make sure that the DepNode of some node coincides with the HirId // owner of that node. @@ -181,7 +142,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { } } - self.insert_entry(hir_id, entry, hash); + let nodes = self.map[hir_id.owner].as_mut().unwrap(); + + debug_assert_eq!(self.parent_node.owner, self.current_dep_node_owner); + insert_vec_map( + &mut nodes.nodes, + hir_id.local_id, + ParentedNode { parent: self.parent_node.local_id, node: node }, + ); } fn with_parent(&mut self, parent_node_id: HirId, f: F) { @@ -191,21 +159,15 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { self.parent_node = parent_node; } - fn with_dep_node_owner< - T: for<'b> HashStable>, - F: FnOnce(&mut Self, Fingerprint), - >( - &mut self, - dep_node_owner: LocalDefId, - item_like: &T, - f: F, - ) { + fn with_dep_node_owner(&mut self, dep_node_owner: LocalDefId, f: impl FnOnce(&mut Self)) { let prev_owner = self.current_dep_node_owner; - let hash = hash_body(&mut self.hcx, item_like); + let prev_parent = self.parent_node; self.current_dep_node_owner = dep_node_owner; - f(self, hash); + self.parent_node = HirId::make_owner(dep_node_owner); + f(self); self.current_dep_node_owner = prev_owner; + self.parent_node = prev_parent; } fn insert_nested(&mut self, item: LocalDefId) { @@ -271,28 +233,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_item(&mut self, i: &'hir Item<'hir>) { debug!("visit_item: {:?}", i); - self.with_dep_node_owner(i.def_id, i, |this, hash| { - let hir_id = i.hir_id(); - this.insert_with_hash(i.span, hir_id, Node::Item(i), hash); - this.with_parent(hir_id, |this| { - if let ItemKind::Struct(ref struct_def, _) = i.kind { - // If this is a tuple or unit-like struct, register the constructor. - if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { - this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def)); - } + self.insert_owner(i.def_id, OwnerNode::Item(i)); + self.with_dep_node_owner(i.def_id, |this| { + if let ItemKind::Struct(ref struct_def, _) = i.kind { + // If this is a tuple or unit-like struct, register the constructor. + if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { + this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def)); } - intravisit::walk_item(this, i); - }); + } + intravisit::walk_item(this, i); }); } fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) { - self.with_dep_node_owner(fi.def_id, fi, |this, hash| { - this.insert_with_hash(fi.span, fi.hir_id(), Node::ForeignItem(fi), hash); - - this.with_parent(fi.hir_id(), |this| { - intravisit::walk_foreign_item(this, fi); - }); + self.insert_owner(fi.def_id, OwnerNode::ForeignItem(fi)); + self.with_dep_node_owner(fi.def_id, |this| { + intravisit::walk_foreign_item(this, fi); }); } @@ -302,26 +258,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) { - self.with_parent(param, |this| intravisit::walk_const_param_default(this, ct)) + self.with_parent(param, |this| { + intravisit::walk_const_param_default(this, ct); + }) } fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) { - self.with_dep_node_owner(ti.def_id, ti, |this, hash| { - this.insert_with_hash(ti.span, ti.hir_id(), Node::TraitItem(ti), hash); - - this.with_parent(ti.hir_id(), |this| { - intravisit::walk_trait_item(this, ti); - }); + self.insert_owner(ti.def_id, OwnerNode::TraitItem(ti)); + self.with_dep_node_owner(ti.def_id, |this| { + intravisit::walk_trait_item(this, ti); }); } fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) { - self.with_dep_node_owner(ii.def_id, ii, |this, hash| { - this.insert_with_hash(ii.span, ii.hir_id(), Node::ImplItem(ii), hash); - - this.with_parent(ii.hir_id(), |this| { - intravisit::walk_impl_item(this, ii); - }); + self.insert_owner(ii.def_id, OwnerNode::ImplItem(ii)); + self.with_dep_node_owner(ii.def_id, |this| { + intravisit::walk_impl_item(this, ii); }); } @@ -413,7 +365,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_local(&mut self, l: &'hir Local<'hir>) { self.insert(l.span, l.hir_id, Node::Local(l)); - self.with_parent(l.hir_id, |this| intravisit::walk_local(this, l)) + self.with_parent(l.hir_id, |this| { + intravisit::walk_local(this, l); + }) } fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) { @@ -440,16 +394,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| { self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index }) }); + self.insert_owner(macro_def.def_id, OwnerNode::MacroDef(macro_def)); self.with_parent(parent, |this| { this.insert_nested(macro_def.def_id); - this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| { - this.insert_with_hash( - macro_def.span, - macro_def.hir_id(), - Node::MacroDef(macro_def), - hash, - ); - }) }); } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index edcbce6b09881..c7bab9fca3219 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -313,7 +313,7 @@ impl<'hir> Map<'hir> { pub fn find(&self, id: HirId) -> Option> { if id.local_id == ItemLocalId::from_u32(0) { let owner = self.tcx.hir_owner(id.owner)?; - Some(owner.node) + Some(owner.node.into()) } else { let owner = self.tcx.hir_owner_nodes(id.owner)?; let node = owner.nodes[id.local_id].as_ref()?; diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 3026bf8274d2e..158499bc0aef7 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -33,9 +33,12 @@ pub struct IndexedHir<'hir> { /// Top-level HIR node for current owner. This only contains the node for which /// `HirId::local_id == 0`, and excludes bodies. +/// +/// This struct exists to encapsulate all access to the hir_owner query in this module, and to +/// implement HashStable without hashing bodies. #[derive(Copy, Clone, Debug)] pub struct Owner<'tcx> { - node: Node<'tcx>, + node: OwnerNode<'tcx>, } impl<'a, 'tcx> HashStable> for Owner<'tcx> { @@ -140,7 +143,8 @@ pub fn provide(providers: &mut Providers) { providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id]; providers.hir_owner = |tcx, id| { let owner = tcx.index_hir(()).map[id].as_ref()?; - let node = owner.nodes[ItemLocalId::new(0)].as_ref()?.node; + let node = owner.nodes[ItemLocalId::new(0)].as_ref().unwrap().node; + let node = node.as_owner().unwrap(); // Indexing must ensure it is an OwnerNode. Some(Owner { node }) }; providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].as_deref(); From 6709648d17c1c760e3e20d3170f57a28edee0d88 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 27 Mar 2021 13:56:22 +0100 Subject: [PATCH 4/6] Use more of OwnerNode. --- compiler/rustc_middle/src/hir/map/mod.rs | 97 +++++++++++------------- 1 file changed, 43 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c7bab9fca3219..60dddce2f5bfe 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1,6 +1,6 @@ use self::collector::NodeCollector; -use crate::hir::{AttributeMap, IndexedHir}; +use crate::hir::{AttributeMap, IndexedHir, Owner}; use crate::ty::TyCtxt; use rustc_ast as ast; use rustc_data_structures::fingerprint::Fingerprint; @@ -121,13 +121,13 @@ pub struct ParentOwnerIterator<'map, 'hir> { } impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> { - type Item = (HirId, Node<'hir>); + type Item = (HirId, OwnerNode<'hir>); fn next(&mut self) -> Option { if self.current_id.local_id.index() != 0 { self.current_id.local_id = ItemLocalId::new(0); - if let Some(node) = self.map.find(self.current_id) { - return Some((self.current_id, node)); + if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) { + return Some((self.current_id, node.node)); } } if self.current_id == CRATE_HIR_ID { @@ -144,8 +144,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> { self.current_id = HirId::make_owner(parent_id); // If this `HirId` doesn't have an entry, skip it and look for its `parent_id`. - if let Some(node) = self.map.find(self.current_id) { - return Some((self.current_id, node)); + if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) { + return Some((self.current_id, node.node)); } } } @@ -331,10 +331,12 @@ impl<'hir> Map<'hir> { } pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { - self.get_if_local(id).and_then(|node| match &node { - Node::ImplItem(impl_item) => Some(&impl_item.generics), - Node::TraitItem(trait_item) => Some(&trait_item.generics), - Node::Item(Item { + let id = id.as_local()?; + let node = self.tcx.hir_owner(id)?; + match node.node { + OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics), + OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics), + OwnerNode::Item(Item { kind: ItemKind::Fn(_, generics, _) | ItemKind::TyAlias(_, generics) @@ -347,35 +349,23 @@ impl<'hir> Map<'hir> { .. }) => Some(generics), _ => None, - }) + } } pub fn item(&self, id: ItemId) -> &'hir Item<'hir> { - match self.find(id.hir_id()).unwrap() { - Node::Item(item) => item, - _ => bug!(), - } + self.tcx.hir_owner(id.def_id).unwrap().node.expect_item() } pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> { - match self.find(id.hir_id()).unwrap() { - Node::TraitItem(item) => item, - _ => bug!(), - } + self.tcx.hir_owner(id.def_id).unwrap().node.expect_trait_item() } pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> { - match self.find(id.hir_id()).unwrap() { - Node::ImplItem(item) => item, - _ => bug!(), - } + self.tcx.hir_owner(id.def_id).unwrap().node.expect_impl_item() } pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> { - match self.find(id.hir_id()).unwrap() { - Node::ForeignItem(item) => item, - _ => bug!(), - } + self.tcx.hir_owner(id.def_id).unwrap().node.expect_foreign_item() } pub fn body(&self, id: BodyId) -> &'hir Body<'hir> { @@ -519,10 +509,12 @@ impl<'hir> Map<'hir> { } pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { - let hir_id = self.local_def_id_to_hir_id(module); - match self.get(hir_id) { - Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id), - Node::Crate(item) => (&item, item.inner, hir_id), + let hir_id = HirId::make_owner(module); + match self.tcx.hir_owner(module).map(|o| o.node) { + Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => { + (m, span, hir_id) + } + Some(OwnerNode::Crate(item)) => (item, item.inner, hir_id), node => panic!("not a module: {:?}", node), } } @@ -659,24 +651,20 @@ impl<'hir> Map<'hir> { /// in the HIR which is recorded by the map and is an item, either an item /// in a module, trait, or impl. pub fn get_parent_item(&self, hir_id: HirId) -> HirId { - for (hir_id, node) in self.parent_owner_iter(hir_id) { - if let Node::Crate(_) - | Node::Item(_) - | Node::ForeignItem(_) - | Node::TraitItem(_) - | Node::ImplItem(_) = node - { - return hir_id; - } + if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() { + // A MacroDef does not have children. + debug_assert!(!matches!(_node, OwnerNode::MacroDef(_))); + hir_id + } else { + CRATE_HIR_ID } - CRATE_HIR_ID } /// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no /// module parent is in this map. pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId { for (hir_id, node) in self.parent_owner_iter(hir_id) { - if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { + if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { return hir_id; } } @@ -749,8 +737,9 @@ impl<'hir> Map<'hir> { pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi { let parent = self.get_parent_item(hir_id); - if let Some(node) = self.find(parent) { - if let Node::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node { + if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) { + if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node + { return *abi; } } @@ -758,22 +747,22 @@ impl<'hir> Map<'hir> { } pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> { - match self.find(id) { - Some(Node::Item(item)) => item, + match self.tcx.hir_owner(id.expect_owner()) { + Some(Owner { node: OwnerNode::Item(item) }) => item, _ => bug!("expected item, found {}", self.node_to_string(id)), } } pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> { - match self.find(id) { - Some(Node::ImplItem(item)) => item, + match self.tcx.hir_owner(id.expect_owner()) { + Some(Owner { node: OwnerNode::ImplItem(item) }) => item, _ => bug!("expected impl item, found {}", self.node_to_string(id)), } } pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> { - match self.find(id) { - Some(Node::TraitItem(item)) => item, + match self.tcx.hir_owner(id.expect_owner()) { + Some(Owner { node: OwnerNode::TraitItem(item) }) => item, _ => bug!("expected trait item, found {}", self.node_to_string(id)), } } @@ -786,15 +775,15 @@ impl<'hir> Map<'hir> { } pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> { - match self.find(id) { - Some(Node::ForeignItem(item)) => item, + match self.tcx.hir_owner(id.expect_owner()) { + Some(Owner { node: OwnerNode::ForeignItem(item) }) => item, _ => bug!("expected foreign item, found {}", self.node_to_string(id)), } } pub fn expect_macro_def(&self, id: HirId) -> &'hir MacroDef<'hir> { - match self.find(id) { - Some(Node::MacroDef(macro_def)) => macro_def, + match self.tcx.hir_owner(id.expect_owner()) { + Some(Owner { node: OwnerNode::MacroDef(macro_def) }) => macro_def, _ => bug!("expected macro def, found {}", self.node_to_string(id)), } } From da43aa61c1853427439d7abf46d03b381f7ab10f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 29 Mar 2021 20:10:26 +0200 Subject: [PATCH 5/6] Bless tests. --- .../ui/associated-item/issue-48027.stderr | 30 +++---- .../hr-associated-type-bound-param-2.stderr | 16 ++-- .../const-param-elided-lifetime.min.stderr | 18 ++-- .../dep-graph-struct-signature.stderr | 72 ++++++++-------- .../ui/dep-graph/dep-graph-type-alias.stderr | 36 ++++---- ...te-arbitrary_self_types-raw-pointer.stderr | 12 +-- src/test/ui/issues/issue-20413.stderr | 82 +++++++++---------- ...adt-param-with-implicit-sized-bound.stderr | 38 ++++----- ...unsafe-trait-should-use-where-sized.stderr | 18 ++-- 9 files changed, 161 insertions(+), 161 deletions(-) diff --git a/src/test/ui/associated-item/issue-48027.stderr b/src/test/ui/associated-item/issue-48027.stderr index 33ab4bb967a97..77915a80a79b0 100644 --- a/src/test/ui/associated-item/issue-48027.stderr +++ b/src/test/ui/associated-item/issue-48027.stderr @@ -1,18 +1,3 @@ -error[E0038]: the trait `Bar` cannot be made into an object - --> $DIR/issue-48027.rs:6:6 - | -LL | impl dyn Bar {} - | ^^^^^^^ `Bar` cannot be made into an object - | - = help: consider moving `X` to another trait -note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit - --> $DIR/issue-48027.rs:2:11 - | -LL | trait Bar { - | --- this trait cannot be made into an object... -LL | const X: usize; - | ^ ...because it contains this associated `const` - error[E0283]: type annotations needed --> $DIR/issue-48027.rs:3:32 | @@ -30,6 +15,21 @@ note: required by `Bar::X` LL | const X: usize; | ^^^^^^^^^^^^^^^ +error[E0038]: the trait `Bar` cannot be made into an object + --> $DIR/issue-48027.rs:6:6 + | +LL | impl dyn Bar {} + | ^^^^^^^ `Bar` cannot be made into an object + | + = help: consider moving `X` to another trait +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-48027.rs:2:11 + | +LL | trait Bar { + | --- this trait cannot be made into an object... +LL | const X: usize; + | ^ ...because it contains this associated `const` + error: aborting due to 2 previous errors Some errors have detailed explanations: E0038, E0283. diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr index 0afa340f7a148..853705dae09f2 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr @@ -14,31 +14,31 @@ LL | for<'b> >::W: Clone, <&T as Clone> error[E0277]: the trait bound `for<'b> >::W: Clone` is not satisfied - --> $DIR/hr-associated-type-bound-param-2.rs:16:14 + --> $DIR/hr-associated-type-bound-param-2.rs:4:8 | LL | trait Z<'a, T: ?Sized> | - required by a bound in this +LL | where +LL | T: Z<'a, u16>, + | ^^^^^^^^^^ the trait `for<'b> Clone` is not implemented for `>::W` ... LL | for<'b> >::W: Clone, | ----- required by this bound in `Z` -... -LL | type W = str; - | ^^^ the trait `for<'b> Clone` is not implemented for `>::W` | = help: the following implementations were found: <&T as Clone> error[E0277]: the trait bound `for<'b> >::W: Clone` is not satisfied - --> $DIR/hr-associated-type-bound-param-2.rs:4:8 + --> $DIR/hr-associated-type-bound-param-2.rs:16:14 | LL | trait Z<'a, T: ?Sized> | - required by a bound in this -LL | where -LL | T: Z<'a, u16>, - | ^^^^^^^^^^ the trait `for<'b> Clone` is not implemented for `>::W` ... LL | for<'b> >::W: Clone, | ----- required by this bound in `Z` +... +LL | type W = str; + | ^^^ the trait `for<'b> Clone` is not implemented for `>::W` | = help: the following implementations were found: <&T as Clone> diff --git a/src/test/ui/const-generics/const-param-elided-lifetime.min.stderr b/src/test/ui/const-generics/const-param-elided-lifetime.min.stderr index 48d33a785aead..613918f78f36c 100644 --- a/src/test/ui/const-generics/const-param-elided-lifetime.min.stderr +++ b/src/test/ui/const-generics/const-param-elided-lifetime.min.stderr @@ -47,28 +47,28 @@ LL | impl A { = help: more complex types are supported with `#![feature(const_generics)]` error: `&'static u8` is forbidden as the type of a const generic parameter - --> $DIR/const-param-elided-lifetime.rs:23:15 + --> $DIR/const-param-elided-lifetime.rs:18:21 | -LL | impl B for A {} - | ^^^ +LL | fn foo(&self) {} + | ^^^ | = note: the only supported types are integers, `bool` and `char` = help: more complex types are supported with `#![feature(const_generics)]` error: `&'static u8` is forbidden as the type of a const generic parameter - --> $DIR/const-param-elided-lifetime.rs:27:17 + --> $DIR/const-param-elided-lifetime.rs:23:15 | -LL | fn bar() {} - | ^^^ +LL | impl B for A {} + | ^^^ | = note: the only supported types are integers, `bool` and `char` = help: more complex types are supported with `#![feature(const_generics)]` error: `&'static u8` is forbidden as the type of a const generic parameter - --> $DIR/const-param-elided-lifetime.rs:18:21 + --> $DIR/const-param-elided-lifetime.rs:27:17 | -LL | fn foo(&self) {} - | ^^^ +LL | fn bar() {} + | ^^^ | = note: the only supported types are integers, `bool` and `char` = help: more complex types are supported with `#![feature(const_generics)]` diff --git a/src/test/ui/dep-graph/dep-graph-struct-signature.stderr b/src/test/ui/dep-graph/dep-graph-struct-signature.stderr index 9d1644a00d002..b81aeabab7ff1 100644 --- a/src/test/ui/dep-graph/dep-graph-struct-signature.stderr +++ b/src/test/ui/dep-graph/dep-graph-struct-signature.stderr @@ -16,6 +16,12 @@ error: no path from `WillChange` to `trait_def` LL | #[rustc_then_this_would_need(trait_def)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: OK + --> $DIR/dep-graph-struct-signature.rs:31:9 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: OK --> $DIR/dep-graph-struct-signature.rs:35:5 | @@ -46,12 +52,36 @@ error: OK LL | #[rustc_then_this_would_need(type_of)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: OK + --> $DIR/dep-graph-struct-signature.rs:47:9 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: OK + --> $DIR/dep-graph-struct-signature.rs:48:9 + | +LL | #[rustc_then_this_would_need(typeck)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: OK --> $DIR/dep-graph-struct-signature.rs:52:5 | LL | #[rustc_then_this_would_need(type_of)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: OK + --> $DIR/dep-graph-struct-signature.rs:54:9 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: OK + --> $DIR/dep-graph-struct-signature.rs:55:9 + | +LL | #[rustc_then_this_would_need(typeck)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: OK --> $DIR/dep-graph-struct-signature.rs:60:9 | @@ -76,6 +106,12 @@ error: no path from `WillChange` to `type_of` LL | #[rustc_then_this_would_need(type_of)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: no path from `WillChange` to `fn_sig` + --> $DIR/dep-graph-struct-signature.rs:76:9 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: no path from `WillChange` to `fn_sig` --> $DIR/dep-graph-struct-signature.rs:80:5 | @@ -94,41 +130,5 @@ error: no path from `WillChange` to `typeck` LL | #[rustc_then_this_would_need(typeck)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: OK - --> $DIR/dep-graph-struct-signature.rs:31:9 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: no path from `WillChange` to `fn_sig` - --> $DIR/dep-graph-struct-signature.rs:76:9 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-struct-signature.rs:47:9 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-struct-signature.rs:48:9 - | -LL | #[rustc_then_this_would_need(typeck)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-struct-signature.rs:54:9 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-struct-signature.rs:55:9 - | -LL | #[rustc_then_this_would_need(typeck)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: aborting due to 22 previous errors diff --git a/src/test/ui/dep-graph/dep-graph-type-alias.stderr b/src/test/ui/dep-graph/dep-graph-type-alias.stderr index 9baaf746fc210..e698ce8f628b4 100644 --- a/src/test/ui/dep-graph/dep-graph-type-alias.stderr +++ b/src/test/ui/dep-graph/dep-graph-type-alias.stderr @@ -28,12 +28,30 @@ error: no path from `TypeAlias` to `type_of` LL | #[rustc_then_this_would_need(type_of)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: OK + --> $DIR/dep-graph-type-alias.rs:35:5 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: no path from `TypeAlias` to `type_of` --> $DIR/dep-graph-type-alias.rs:41:1 | LL | #[rustc_then_this_would_need(type_of)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: OK + --> $DIR/dep-graph-type-alias.rs:43:5 + | +LL | #[rustc_then_this_would_need(fn_sig)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: OK + --> $DIR/dep-graph-type-alias.rs:44:5 + | +LL | #[rustc_then_this_would_need(typeck)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: OK --> $DIR/dep-graph-type-alias.rs:48:1 | @@ -52,23 +70,5 @@ error: OK LL | #[rustc_then_this_would_need(typeck)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: OK - --> $DIR/dep-graph-type-alias.rs:35:5 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-type-alias.rs:43:5 - | -LL | #[rustc_then_this_would_need(fn_sig)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: OK - --> $DIR/dep-graph-type-alias.rs:44:5 - | -LL | #[rustc_then_this_would_need(typeck)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: aborting due to 12 previous errors diff --git a/src/test/ui/feature-gates/feature-gate-arbitrary_self_types-raw-pointer.stderr b/src/test/ui/feature-gates/feature-gate-arbitrary_self_types-raw-pointer.stderr index a80f9befcafdc..f9c53a66c4b84 100644 --- a/src/test/ui/feature-gates/feature-gate-arbitrary_self_types-raw-pointer.stderr +++ b/src/test/ui/feature-gates/feature-gate-arbitrary_self_types-raw-pointer.stderr @@ -1,17 +1,17 @@ -error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature - --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:9:18 +error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types` feature + --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:4:18 | -LL | fn bar(self: *const Self); +LL | fn foo(self: *const Self) {} | ^^^^^^^^^^^ | = note: see issue #44874 for more information = help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable = help: consider changing to `self`, `&self`, `&mut self`, `self: Box`, `self: Rc`, `self: Arc`, or `self: Pin

` (where P is one of the previous types except `Self`) -error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types` feature - --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:4:18 +error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature + --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:9:18 | -LL | fn foo(self: *const Self) {} +LL | fn bar(self: *const Self); | ^^^^^^^^^^^ | = note: see issue #44874 for more information diff --git a/src/test/ui/issues/issue-20413.stderr b/src/test/ui/issues/issue-20413.stderr index 6bcff7aff2dc4..28f87a75f0f15 100644 --- a/src/test/ui/issues/issue-20413.stderr +++ b/src/test/ui/issues/issue-20413.stderr @@ -25,6 +25,24 @@ LL | impl Foo for T where NoData: Foo { = note: 127 redundant requirements hidden = note: required because of the requirements on the impl of `Foo` for `NoData` +error[E0275]: overflow evaluating the requirement `NoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Foo` + --> $DIR/issue-20413.rs:8:36 + | +LL | trait Foo { + | --------- required by this bound in `Foo` +... +LL | impl Foo for T where NoData: Foo { + | ^^^ + | + = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_20413`) +note: required because of the requirements on the impl of `Foo` for `NoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` + --> $DIR/issue-20413.rs:8:9 + | +LL | impl Foo for T where NoData: Foo { + | ^^^ ^ + = note: 127 redundant requirements hidden + = note: required because of the requirements on the impl of `Foo` for `NoData` + error[E0275]: overflow evaluating the requirement `EvenLessData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Baz` --> $DIR/issue-20413.rs:28:42 | @@ -48,47 +66,6 @@ LL | impl Baz for T where AlmostNoData: Bar { = note: 126 redundant requirements hidden = note: required because of the requirements on the impl of `Baz` for `EvenLessData` -error[E0275]: overflow evaluating the requirement `AlmostNoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Bar` - --> $DIR/issue-20413.rs:36:42 - | -LL | trait Bar { - | --------- required by this bound in `Bar` -... -LL | impl Baz for T where AlmostNoData: Bar { - | ^^^ - | - = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_20413`) -note: required because of the requirements on the impl of `Baz` for `EvenLessData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` - --> $DIR/issue-20413.rs:36:9 - | -LL | impl Baz for T where AlmostNoData: Bar { - | ^^^ ^ -note: required because of the requirements on the impl of `Bar` for `AlmostNoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` - --> $DIR/issue-20413.rs:28:9 - | -LL | impl Bar for T where EvenLessData: Baz { - | ^^^ ^ - = note: 126 redundant requirements hidden - = note: required because of the requirements on the impl of `Bar` for `AlmostNoData` - -error[E0275]: overflow evaluating the requirement `NoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Foo` - --> $DIR/issue-20413.rs:8:36 - | -LL | trait Foo { - | --------- required by this bound in `Foo` -... -LL | impl Foo for T where NoData: Foo { - | ^^^ - | - = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_20413`) -note: required because of the requirements on the impl of `Foo` for `NoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` - --> $DIR/issue-20413.rs:8:9 - | -LL | impl Foo for T where NoData: Foo { - | ^^^ ^ - = note: 127 redundant requirements hidden - = note: required because of the requirements on the impl of `Foo` for `NoData` - error[E0275]: overflow evaluating the requirement `EvenLessData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Baz` --> $DIR/issue-20413.rs:28:42 | @@ -135,6 +112,29 @@ LL | impl Bar for T where EvenLessData: Baz { = note: 126 redundant requirements hidden = note: required because of the requirements on the impl of `Bar` for `AlmostNoData` +error[E0275]: overflow evaluating the requirement `AlmostNoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>: Bar` + --> $DIR/issue-20413.rs:36:42 + | +LL | trait Bar { + | --------- required by this bound in `Bar` +... +LL | impl Baz for T where AlmostNoData: Bar { + | ^^^ + | + = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_20413`) +note: required because of the requirements on the impl of `Baz` for `EvenLessData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` + --> $DIR/issue-20413.rs:36:9 + | +LL | impl Baz for T where AlmostNoData: Bar { + | ^^^ ^ +note: required because of the requirements on the impl of `Bar` for `AlmostNoData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` + --> $DIR/issue-20413.rs:28:9 + | +LL | impl Bar for T where EvenLessData: Baz { + | ^^^ ^ + = note: 126 redundant requirements hidden + = note: required because of the requirements on the impl of `Bar` for `AlmostNoData` + error: aborting due to 7 previous errors Some errors have detailed explanations: E0275, E0392. diff --git a/src/test/ui/suggestions/adt-param-with-implicit-sized-bound.stderr b/src/test/ui/suggestions/adt-param-with-implicit-sized-bound.stderr index 5cb3a404037a7..18ba7254446b4 100644 --- a/src/test/ui/suggestions/adt-param-with-implicit-sized-bound.stderr +++ b/src/test/ui/suggestions/adt-param-with-implicit-sized-bound.stderr @@ -1,22 +1,3 @@ -error[E0277]: the size for values of type `T` cannot be known at compilation time - --> $DIR/adt-param-with-implicit-sized-bound.rs:25:9 - | -LL | struct X(T); - | - required by this bound in `X` -... -LL | struct Struct5{ - | - this type parameter needs to be `std::marker::Sized` -LL | _t: X, - | ^^^^ doesn't have a size known at compile-time - | -help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box` - --> $DIR/adt-param-with-implicit-sized-bound.rs:18:10 - | -LL | struct X(T); - | ^ - ...if indirection were used here: `Box` - | | - | this could be changed to `T: ?Sized`... - error[E0277]: the size for values of type `Self` cannot be known at compilation time --> $DIR/adt-param-with-implicit-sized-bound.rs:2:19 | @@ -92,6 +73,25 @@ help: consider relaxing the implicit `Sized` restriction LL | struct Struct4{ | ^^^^^^^^ +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/adt-param-with-implicit-sized-bound.rs:25:9 + | +LL | struct X(T); + | - required by this bound in `X` +... +LL | struct Struct5{ + | - this type parameter needs to be `std::marker::Sized` +LL | _t: X, + | ^^^^ doesn't have a size known at compile-time + | +help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box` + --> $DIR/adt-param-with-implicit-sized-bound.rs:18:10 + | +LL | struct X(T); + | ^ - ...if indirection were used here: `Box` + | | + | this could be changed to `T: ?Sized`... + error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr index 4c18f6d79d077..4a53706f9772e 100644 --- a/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr +++ b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr @@ -1,3 +1,12 @@ +error[E0307]: invalid `self` parameter type: () + --> $DIR/object-unsafe-trait-should-use-where-sized.rs:6:18 + | +LL | fn bar(self: ()) {} + | ^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box`, `self: Rc`, `self: Arc`, or `self: Pin

` (where P is one of the previous types except `Self`) + error[E0038]: the trait `Trait` cannot be made into an object --> $DIR/object-unsafe-trait-should-use-where-sized.rs:9:12 | @@ -26,15 +35,6 @@ help: consider changing method `bar`'s `self` parameter to be `&self` LL | fn bar(self: &Self) {} | ^^^^^ -error[E0307]: invalid `self` parameter type: () - --> $DIR/object-unsafe-trait-should-use-where-sized.rs:6:18 - | -LL | fn bar(self: ()) {} - | ^^ - | - = note: type of `self` must be `Self` or a type that dereferences to it - = help: consider changing to `self`, `&self`, `&mut self`, `self: Box`, `self: Rc`, `self: Arc`, or `self: Pin

` (where P is one of the previous types except `Self`) - error: aborting due to 2 previous errors Some errors have detailed explanations: E0038, E0307. From f798510d02005c2bbb1269c384c321918c578f74 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 24 Jul 2021 19:30:46 +0200 Subject: [PATCH 6/6] Only check macro attributes when checking the crate root. --- compiler/rustc_passes/src/check_attr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 71231830e99a7..ee9057a3bab77 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1644,11 +1644,11 @@ fn check_invalid_macro_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) { fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { let check_attr_visitor = &mut CheckAttrVisitor { tcx }; tcx.hir().visit_item_likes_in_module(module_def_id, &mut check_attr_visitor.as_deep_visitor()); - tcx.hir().visit_exported_macros_in_krate(check_attr_visitor); - check_invalid_macro_level_attr(tcx, tcx.hir().krate().non_exported_macro_attrs); if module_def_id.is_top_level_module() { check_attr_visitor.check_attributes(CRATE_HIR_ID, &DUMMY_SP, Target::Mod, None); check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs()); + tcx.hir().visit_exported_macros_in_krate(check_attr_visitor); + check_invalid_macro_level_attr(tcx, tcx.hir().krate().non_exported_macro_attrs); } }