From 748c4889e164fa69119e6afed3bc3abcf7463c2e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 16:47:41 +0200 Subject: [PATCH 1/8] Add `DefId.is_local` --- frontend/exporter/src/types/copied.rs | 4 +++- frontend/exporter/src/types/def_id.rs | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/frontend/exporter/src/types/copied.rs b/frontend/exporter/src/types/copied.rs index 1b5c8ba07..03b7fee14 100644 --- a/frontend/exporter/src/types/copied.rs +++ b/frontend/exporter/src/types/copied.rs @@ -10,7 +10,8 @@ impl std::hash::Hash for DefId { let DefId { krate, path, - index: _, // intentionally discarding index + index: _, // intentionally discarding index + is_local: _, // intentionally discarding is_local } = self; krate.hash(state); path.hash(state); @@ -31,6 +32,7 @@ impl<'s, S: BaseState<'s>> SInto for rustc_hir::def_id::DefId { rustc_hir::def_id::CrateNum::as_u32(self.krate), rustc_hir::def_id::DefIndex::as_u32(self.index), ), + is_local: self.is_local(), } } } diff --git a/frontend/exporter/src/types/def_id.rs b/frontend/exporter/src/types/def_id.rs index cbb38a802..2fe462f5e 100644 --- a/frontend/exporter/src/types/def_id.rs +++ b/frontend/exporter/src/types/def_id.rs @@ -46,6 +46,7 @@ pub struct DefId { /// valid only for one Rustc sesssion. Please do not rely on those /// indexes unless you cannot do otherwise. pub index: (u32, u32), + pub is_local: bool, } /// Reflects [`rustc_hir::definitions::DefPathData`] From 16e6bcd29ff25dcc0bc11b576ce8aefdb64f4024 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 16:50:40 +0200 Subject: [PATCH 2/8] Record attributes in `FullDef` --- frontend/exporter/src/types/new/full_def.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frontend/exporter/src/types/new/full_def.rs b/frontend/exporter/src/types/new/full_def.rs index 0711fc4cc..6b8cf08d7 100644 --- a/frontend/exporter/src/types/new/full_def.rs +++ b/frontend/exporter/src/types/new/full_def.rs @@ -17,6 +17,9 @@ pub struct FullDef { pub parent: Option, #[value(s.base().tcx.def_span(*self).sinto(s))] pub span: Span, + #[value(s.base().tcx.get_attrs_unchecked(*self).sinto(s))] + /// Attributes on this definition, if applicable. + pub attributes: Vec, #[value({ let state_with_id = State { thir: (), mir: (), owner_id: *self, base: s.base() }; s.base().tcx.def_kind(*self).sinto(&state_with_id) From a611058bc51f294573f0bbb737b16bb8834577d5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 16:48:53 +0200 Subject: [PATCH 3/8] Record the string representation of attributes --- engine/lib/import_thir.ml | 4 ++-- frontend/exporter/src/types/copied.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/engine/lib/import_thir.ml b/engine/lib/import_thir.ml index 4cef3c46e..852153147 100644 --- a/engine/lib/import_thir.ml +++ b/engine/lib/import_thir.ml @@ -109,12 +109,12 @@ let c_attr (attr : Thir.attribute) : attr = | Normal { item = - { args = Eq (_, Hir { symbol; _ }); path = "doc"; tokens = None }; + { args = Eq (_, Hir { symbol; _ }); path = "doc"; tokens = None; _ }; tokens = None; } -> DocComment { kind = DCKLine; body = symbol } (* Looks for `#[doc = "something"]` *) - | Normal { item = { args; path; tokens = subtokens }; tokens } -> + | Normal { item = { args; path; tokens = subtokens; _ }; tokens } -> let args_tokens = match args with Delimited { tokens; _ } -> Some tokens | _ -> None in diff --git a/frontend/exporter/src/types/copied.rs b/frontend/exporter/src/types/copied.rs index 03b7fee14..d0e88f731 100644 --- a/frontend/exporter/src/types/copied.rs +++ b/frontend/exporter/src/types/copied.rs @@ -109,7 +109,7 @@ pub enum AttrStyle { /// Reflects [`rustc_ast::ast::Attribute`] #[derive_group(Serializers)] #[derive(AdtInto, Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)] -#[args(<'slt, S: BaseState<'slt>>, from: rustc_ast::ast::Attribute, state: S as gstate)] +#[args(<'tcx, S: BaseState<'tcx>>, from: rustc_ast::ast::Attribute, state: S as gstate)] pub struct Attribute { pub kind: AttrKind, #[map(x.as_usize())] @@ -2368,6 +2368,15 @@ pub struct AttrItem { pub path: String, pub args: AttrArgs, pub tokens: Option, + /// String representation of the item. This may not exactly match what the user wrote because + /// the whitespace is parsed and pretty-printed. + #[value({ + use rustc_ast_pretty::pprust::PrintState; + rustc_ast_pretty::pprust::State::to_string(|s| { + s.print_attr_item(self, rustc_span::DUMMY_SP) + }) + })] + pub as_str: String, } #[cfg(feature = "rustc")] From bb103f1f0fe7905c939f7aab28116bcf0c3c5f5a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 16:50:13 +0200 Subject: [PATCH 4/8] Record inlineability of functions --- frontend/exporter/src/lib.rs | 1 + frontend/exporter/src/types/copied.rs | 11 +++++++++++ frontend/exporter/src/types/new/full_def.rs | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/frontend/exporter/src/lib.rs b/frontend/exporter/src/lib.rs index 523b2eac3..aefe44096 100644 --- a/frontend/exporter/src/lib.rs +++ b/frontend/exporter/src/lib.rs @@ -22,6 +22,7 @@ cfg_feature_rustc! { extern crate rustc_abi; extern crate rustc_ast; extern crate rustc_ast_pretty; + extern crate rustc_attr; extern crate rustc_data_structures; extern crate rustc_driver; extern crate rustc_errors; diff --git a/frontend/exporter/src/types/copied.rs b/frontend/exporter/src/types/copied.rs index d0e88f731..5e0374c54 100644 --- a/frontend/exporter/src/types/copied.rs +++ b/frontend/exporter/src/types/copied.rs @@ -118,6 +118,17 @@ pub struct Attribute { pub span: Span, } +/// Reflects [`rustc_attr::InlineAttr`] +#[derive_group(Serializers)] +#[derive(AdtInto, Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[args(<'tcx, S: BaseState<'tcx>>, from: rustc_attr::InlineAttr, state: S as _s)] +pub enum InlineAttr { + None, + Hint, + Always, + Never, +} + /// Generic container for decorating items with a type, a span, /// attributes and other meta-data. #[derive_group(Serializers)] diff --git a/frontend/exporter/src/types/new/full_def.rs b/frontend/exporter/src/types/new/full_def.rs index 6b8cf08d7..1acc63f34 100644 --- a/frontend/exporter/src/types/new/full_def.rs +++ b/frontend/exporter/src/types/new/full_def.rs @@ -128,6 +128,8 @@ pub enum FullDefKind { generics: TyGenerics, #[value(get_generic_predicates(s, s.owner_id()))] predicates: GenericPredicates, + #[value(s.base().tcx.codegen_fn_attrs(s.owner_id()).inline.sinto(s))] + inline: InlineAttr, #[value(s.base().tcx.fn_sig(s.owner_id()).instantiate_identity().sinto(s))] sig: PolyFnSig, }, @@ -142,6 +144,8 @@ pub enum FullDefKind { generics: TyGenerics, #[value(get_generic_predicates(s, s.owner_id()))] predicates: GenericPredicates, + #[value(s.base().tcx.codegen_fn_attrs(s.owner_id()).inline.sinto(s))] + inline: InlineAttr, #[value(s.base().tcx.fn_sig(s.owner_id()).instantiate_identity().sinto(s))] sig: PolyFnSig, }, From e826537acd55404ebaf0bc7954f0164fe7413395 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 16:50:52 +0200 Subject: [PATCH 5/8] Record item visibility in `FullDef` --- frontend/exporter/src/types/new/full_def.rs | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/frontend/exporter/src/types/new/full_def.rs b/frontend/exporter/src/types/new/full_def.rs index 1acc63f34..6f07671df 100644 --- a/frontend/exporter/src/types/new/full_def.rs +++ b/frontend/exporter/src/types/new/full_def.rs @@ -20,6 +20,9 @@ pub struct FullDef { #[value(s.base().tcx.get_attrs_unchecked(*self).sinto(s))] /// Attributes on this definition, if applicable. pub attributes: Vec, + #[value(get_def_visibility(s, *self))] + /// Visibility of the definition, for definitions where this makes sense. + pub visibility: Option, #[value({ let state_with_id = State { thir: (), mir: (), owner_id: *self, base: s.base() }; s.base().tcx.def_kind(*self).sinto(&state_with_id) @@ -317,6 +320,47 @@ impl FullDef { } } +/// Gets the visibility (`pub` or not) of the definition. Returns `None` for defs that don't have a +/// meaningful visibility. +#[cfg(feature = "rustc")] +fn get_def_visibility<'tcx, S: BaseState<'tcx>>(s: &S, def_id: RDefId) -> Option { + use rustc_hir::def::DefKind::*; + let tcx = s.base().tcx; + match tcx.def_kind(def_id) { + AssocConst + | AssocFn + | Const + | Enum + | Field + | Fn + | ForeignTy + | Macro { .. } + | Mod + | Static { .. } + | Struct + | Trait + | TraitAlias + | TyAlias { .. } + | Union + | Use + | Variant => Some(tcx.visibility(def_id).is_public()), + // These kinds don't have visibility modifiers (which would cause `visibility` to panic). + AnonConst + | AssocTy + | Closure + | ConstParam + | Ctor { .. } + | ExternCrate + | ForeignMod + | GlobalAsm + | Impl { .. } + | InlineConst + | LifetimeParam + | OpaqueTy + | TyParam => None, + } +} + /// This normalizes trait clauses before calling `sinto` on them. This is a bit of a hack required /// by charon for now. We can't normalize all clauses as this would lose region information in /// outlives clauses. From dd7390d0806b3833ed7abe1433801fc66897f925 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 17:10:47 +0200 Subject: [PATCH 6/8] Record lang and diagnostic item identifiers in `FullDef` --- frontend/exporter/src/types/new/full_def.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frontend/exporter/src/types/new/full_def.rs b/frontend/exporter/src/types/new/full_def.rs index 6f07671df..7e4526e35 100644 --- a/frontend/exporter/src/types/new/full_def.rs +++ b/frontend/exporter/src/types/new/full_def.rs @@ -23,6 +23,12 @@ pub struct FullDef { #[value(get_def_visibility(s, *self))] /// Visibility of the definition, for definitions where this makes sense. pub visibility: Option, + #[value(s.base().tcx.as_lang_item(*self).map(|litem| litem.name()).sinto(s))] + /// If this definition is a lang item, we store the identifier, e.g. `sized`. + pub lang_item: Option, + #[value(s.base().tcx.get_diagnostic_name(*self).sinto(s))] + /// If this definition is a diagnostic item, we store the identifier, e.g. `box_new`. + pub diagnostic_item: Option, #[value({ let state_with_id = State { thir: (), mir: (), owner_id: *self, base: s.base() }; s.base().tcx.def_kind(*self).sinto(&state_with_id) From 8249e057f593f511e6cd88f4f52ef0d1ae57d06f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2024 17:31:46 +0200 Subject: [PATCH 7/8] Improve the `DefId: Debug` impl when rustc is available --- frontend/exporter/src/types/def_id.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/frontend/exporter/src/types/def_id.rs b/frontend/exporter/src/types/def_id.rs index 2fe462f5e..517ebd8cd 100644 --- a/frontend/exporter/src/types/def_id.rs +++ b/frontend/exporter/src/types/def_id.rs @@ -32,7 +32,7 @@ pub struct DisambiguatedDefPathItem { /// Reflects [`rustc_hir::def_id::DefId`] #[derive_group(Serializers)] -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(not(feature = "extract_names_mode"), derive(JsonSchema))] pub struct DefId { pub krate: String, @@ -49,6 +49,24 @@ pub struct DefId { pub is_local: bool, } +#[cfg(not(feature = "rustc"))] +impl std::fmt::Debug for DefId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DefId") + .field("krate", &self.krate) + .field("path", &self.path) + .finish() + } +} + +#[cfg(feature = "rustc")] +impl std::fmt::Debug for DefId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // Use the more legible rustc debug implementation. + write!(f, "{:?}", rustc_span::def_id::DefId::from(self)) + } +} + /// Reflects [`rustc_hir::definitions::DefPathData`] #[derive_group(Serializers)] #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] From 9c46ed7122eb928e5ffb074faec23e37f513ebcb Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 29 Aug 2024 13:47:55 +0200 Subject: [PATCH 8/8] Remove ad-hoc `AttrItem.as_str` --- frontend/exporter/src/types/copied.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/frontend/exporter/src/types/copied.rs b/frontend/exporter/src/types/copied.rs index 5e0374c54..4d12199b9 100644 --- a/frontend/exporter/src/types/copied.rs +++ b/frontend/exporter/src/types/copied.rs @@ -2379,15 +2379,6 @@ pub struct AttrItem { pub path: String, pub args: AttrArgs, pub tokens: Option, - /// String representation of the item. This may not exactly match what the user wrote because - /// the whitespace is parsed and pretty-printed. - #[value({ - use rustc_ast_pretty::pprust::PrintState; - rustc_ast_pretty::pprust::State::to_string(|s| { - s.print_attr_item(self, rustc_span::DUMMY_SP) - }) - })] - pub as_str: String, } #[cfg(feature = "rustc")]