Skip to content

Commit

Permalink
Add more information to FullDef
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 14, 2024
1 parent 200fef4 commit 651ae5a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
47 changes: 34 additions & 13 deletions frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3879,25 +3879,46 @@ pub enum PredicateKind {
}

/// Reflects [`rustc_middle::ty::ImplSubject`]
#[derive(AdtInto)]
#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: rustc_middle::ty::ImplSubject<'tcx>, state: S as s)]
#[derive_group(Serializers)]
#[derive(Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum ImplSubject {
Trait(
// Also record the polarity.
#[map({
let polarity = s.base().tcx.impl_polarity(s.owner_id());
TraitPredicate {
trait_ref: x.sinto(s),
is_positive: matches!(polarity, rustc_middle::ty::ImplPolarity::Positive),
}
})]
TraitPredicate,
),
Trait {
/// The trait that is implemented by this impl block.
trait_pred: TraitPredicate,
/// The `ImplExpr`s required to satisfy the predicates on the trait declaration. E.g.:
/// ```ignore
/// trait Foo: Bar {}
/// impl Foo for () {} // would supply an `ImplExpr` for `Self: Bar`.
/// ```
required_impl_exprs: Vec<ImplExpr>,
},
Inherent(Ty),
}

#[cfg(feature = "rustc")]
impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ImplSubject> for ty::ImplSubject<'tcx> {
fn sinto(&self, s: &S) -> ImplSubject {
let tcx = s.base().tcx;
match self {
ty::ImplSubject::Inherent(ty) => ImplSubject::Inherent(ty.sinto(s)),
ty::ImplSubject::Trait(trait_ref) => {
// Also record the polarity.
let polarity = tcx.impl_polarity(s.owner_id());
let trait_pred = TraitPredicate {
trait_ref: trait_ref.sinto(s),
is_positive: matches!(polarity, rustc_middle::ty::ImplPolarity::Positive),
};
let required_impl_exprs =
solve_item_traits(s, trait_ref.def_id, trait_ref.args, None);
ImplSubject::Trait {
trait_pred,
required_impl_exprs,
}
}
}
}
}

/// Reflects [`rustc_hir::GenericBounds`]
type GenericBounds = Vec<Clause>;

Expand Down
19 changes: 18 additions & 1 deletion frontend/exporter/src/types/new/full_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ pub struct FullDef {
pub def_id: DefId,
/// The enclosing item.
pub parent: Option<DefId>,
/// The span of the definition of this item (e.g. for a function this is is signature).
pub span: Span,
/// The span of the whole definition (including e.g. the function body).
pub source_span: Option<Span>,
/// The text of the whole definition.
pub source_text: Option<String>,
/// Attributes on this definition, if applicable.
pub attributes: Vec<Attribute>,
/// Visibility of the definition, for definitions where this makes sense.
Expand All @@ -37,10 +42,17 @@ impl<'tcx, S: BaseState<'tcx>> SInto<S, Arc<FullDef>> for RDefId {
let state_with_id = with_owner_id(s.base(), (), (), def_id);
tcx.def_kind(def_id).sinto(&state_with_id)
};

let source_span = def_id.as_local().map(|ldid| tcx.source_span(ldid));
let source_text = source_span
.filter(|source_span| source_span.ctxt().is_root())
.and_then(|source_span| tcx.sess.source_map().span_to_snippet(source_span).ok());
let full_def = FullDef {
def_id: self.sinto(s),
parent: tcx.opt_parent(def_id).sinto(s),
span: tcx.def_span(def_id).sinto(s),
source_span: source_span.sinto(s),
source_text,
attributes: tcx.get_attrs_unchecked(def_id).sinto(s),
visibility: get_def_visibility(tcx, def_id),
lang_item: s
Expand Down Expand Up @@ -99,7 +111,7 @@ pub enum FullDefKind {
generics: TyGenerics,
#[value(get_generic_predicates(s, s.owner_id()))]
predicates: GenericPredicates,
// `predicates_of` has the special `Self: Trait` clause as its last element.
/// The special `Self: Trait` clause.
#[value({
use ty::Upcast;
let tcx = s.base().tcx;
Expand Down Expand Up @@ -294,6 +306,11 @@ pub enum FullDefKind {
}

impl FullDef {
#[cfg(feature = "rustc")]
pub fn rust_def_id(&self) -> RDefId {
(&self.def_id).into()
}

pub fn kind(&self) -> &FullDefKind {
&self.kind
}
Expand Down

0 comments on commit 651ae5a

Please sign in to comment.