Skip to content

Commit

Permalink
predicates of
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmarkmartin authored and spastorino committed Aug 23, 2023
1 parent 735e9c0 commit 107cb5c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
38 changes: 28 additions & 10 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ impl<'tcx> Context for Tables<'tcx> {
let generic_def = self.tcx.generics_of(def_id);
generic_def.stable(self)
}

fn predicates_of(
&mut self,
trait_def: &stable_mir::ty::TraitDef,
) -> stable_mir::GenericPredicates {
let trait_def_id = self.trait_def_id(trait_def);
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(trait_def_id);
stable_mir::GenericPredicates {
parent: parent.map(|did| self.trait_def(did)),
predicates: predicates
.iter()
.map(|(clause, span)| {
(clause.as_predicate().kind().skip_binder().stable(self), span.stable(self))
})
.collect(),
}
}
}

pub struct Tables<'tcx> {
Expand Down Expand Up @@ -947,12 +964,12 @@ impl<'tcx> Stable<'tcx> for ty::BoundTyKind {
impl<'tcx> Stable<'tcx> for ty::BoundRegionKind {
type T = stable_mir::ty::BoundRegionKind;

fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::BoundRegionKind;

match self {
ty::BoundRegionKind::BrAnon(option_span) => {
BoundRegionKind::BrAnon(option_span.map(|span| opaque(&span)))
BoundRegionKind::BrAnon(option_span.map(|span| span.stable(tables)))
}
ty::BoundRegionKind::BrNamed(def_id, symbol) => {
BoundRegionKind::BrNamed(rustc_internal::br_named_def(*def_id), symbol.to_string())
Expand Down Expand Up @@ -1242,14 +1259,6 @@ impl<'tcx> Stable<'tcx> for ty::Generics {
}
}

impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = stable_mir::ty::Span;

fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
opaque(self)
}
}

impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
type T = stable_mir::ty::GenericParamDefKind;

Expand Down Expand Up @@ -1456,3 +1465,12 @@ impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
opaque(self)
}
}

impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = stable_mir::ty::Span;

fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
// FIXME: add a real implementation of stable spans
opaque(self)
}
}
15 changes: 14 additions & 1 deletion compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use std::cell::Cell;

use crate::rustc_smir::Tables;

use self::ty::{GenericDef, Generics, ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind};
use self::ty::{
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
};

pub mod mir;
pub mod ty;
Expand All @@ -38,6 +40,12 @@ pub type TraitDecls = Vec<TraitDef>;
/// A list of impl trait decls.
pub type ImplTraitDecls = Vec<ImplDef>;

/// A list of predicates.
pub struct GenericPredicates {
pub parent: Option<TraitDef>,
pub predicates: Vec<(PredicateKind, Span)>,
}

/// Holds information about a crate.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Crate {
Expand Down Expand Up @@ -101,6 +109,10 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
with(|cx| cx.trait_impl(trait_impl))
}

pub fn predicates_of(trait_def: &TraitDef) -> GenericPredicates {
with(|cx| cx.predicates_of(trait_def))
}

pub trait Context {
fn entry_fn(&mut self) -> Option<CrateItem>;
/// Retrieve all items of the local crate that have a MIR associated with them.
Expand All @@ -111,6 +123,7 @@ pub trait Context {
fn all_trait_impls(&mut self) -> ImplTraitDecls;
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
fn generics_of(&mut self, generic_def: &GenericDef) -> Generics;
fn predicates_of(&mut self, trait_def: &TraitDef) -> GenericPredicates;
/// Get information about the local crate.
fn local_crate(&self) -> Crate;
/// Retrieve a list of all external crates.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Const {

type Ident = Opaque;
pub(crate) type Region = Opaque;
pub type Span = Opaque;
pub(crate) type Span = Opaque;

#[derive(Clone, Debug)]
pub enum TyKind {
Expand Down

0 comments on commit 107cb5c

Please sign in to comment.