Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rudimentary support for dynamic trait objects #664

Merged
merged 37 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift click to select a range
57de9c9
add stuff to generic_const test
ranjitjhala May 22, 2024
db4a43a
asd Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jun 14, 2024
73805c6
asd Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jun 17, 2024
d9376ce
as Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jun 25, 2024
2ff58ce
rm
ranjitjhala Jun 25, 2024
ba4f998
as Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jun 27, 2024
c4803d1
ad Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jun 29, 2024
0555720
qwea Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jul 12, 2024
aae8cd2
as Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jul 17, 2024
39ca3ab
ads Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jul 19, 2024
c941f92
asd Merge branch 'main' of github.com:flux-rs/flux
ranjitjhala Jul 19, 2024
a383d2a
next: onto lower type
ranjitjhala Jul 20, 2024
40861ad
next: onto lower type
ranjitjhala Jul 22, 2024
e763acc
next: onto lower type
ranjitjhala Jul 23, 2024
d3af003
next: into constraint_gen
ranjitjhala Jul 23, 2024
961aa8d
next: into constraint_gen
ranjitjhala Jul 23, 2024
942704a
yay, dyn00 works (doesn't keel over)
ranjitjhala Jul 23, 2024
1e4f71c
yay, dyn00 works (doesn't keel over)
ranjitjhala Jul 23, 2024
64650ce
yay, dyn01 checks/fails as expected
ranjitjhala Jul 23, 2024
23c7453
clippy
ranjitjhala Jul 23, 2024
886ad1e
update pseudocode in README
ranjitjhala Jul 23, 2024
8ed155c
allow region vars in dyn
ranjitjhala Jul 23, 2024
193032b
allow region vars in dyn
ranjitjhala Jul 24, 2024
cbeaf3b
allow region vars in dyn
ranjitjhala Jul 24, 2024
eacee3c
Update crates/flux-middle/src/fhir/lift.rs
ranjitjhala Jul 25, 2024
e803419
use Dynamic instead of PolyTraitRef
ranjitjhala Jul 25, 2024
ae2dde8
use Dynamic instead of PolyTraitRef
ranjitjhala Jul 25, 2024
689d4b1
asd Merge branch 'dyn' of github.com:flux-rs/flux into dyn
ranjitjhala Jul 25, 2024
01c4724
remove DynKind (only support Dyn) else panic
ranjitjhala Jul 25, 2024
1acc11c
implement to_rustc for Dynamic/ExistentialPredicates
ranjitjhala Jul 25, 2024
5409fef
Update crates/flux-middle/src/rustc/lowering.rs
ranjitjhala Jul 28, 2024
b60b1a9
check dyn exi-preds are equal during subtyping
ranjitjhala Jul 28, 2024
6a755d5
restore
ranjitjhala Jul 28, 2024
eca76a0
use span_bug
ranjitjhala Jul 28, 2024
fa16a77
dont fill lifetimes in fill_generic_args
ranjitjhala Jul 28, 2024
a83c11d
dont fill lifetimes in fill_generic_args
ranjitjhala Jul 28, 2024
5974bc6
merge in GH edits
ranjitjhala Jul 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove DynKind (only support Dyn) else panic
  • Loading branch information
ranjitjhala committed Jul 25, 2024
commit 01c4724ccd3ab35ed2718e0f311f5c6bce746a2e
4 changes: 2 additions & 2 deletions crates/flux-fhir-analysis/src/conv/fill_holes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 217,8 @@ impl<'genv, 'tcx> Zipper<'genv, 'tcx> {
debug_assert_eq!(pty_a, pty_b);
}
(
rty::BaseTy::Dynamic(poly_trait_refs, re_a, _),
ty::TyKind::Dynamic(poly_trait_refs_b, re_b, _),
rty::BaseTy::Dynamic(poly_trait_refs, re_a),
ty::TyKind::Dynamic(poly_trait_refs_b, re_b),
) => {
self.zip_region(re_a, re_b);
debug_assert_eq!(poly_trait_refs.len(), poly_trait_refs_b.len());
Expand Down
22 changes: 6 additions & 16 deletions crates/flux-fhir-analysis/src/conv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 23,7 @@ use flux_middle::{
refining::{self, Refiner},
AdtSortDef, ESpan, WfckResults, INNERMOST,
},
rustc::{
self,
ty::{DynKind, Region},
},
rustc::{self, ty::Region},
};
use itertools::Itertools;
use rustc_data_structures::fx::FxIndexMap;
Expand Down Expand Up @@ -819,18 816,11 @@ impl<'a, 'genv, 'tcx> ConvCtxt<'a, 'genv, 'tcx> {
.map(|poly_trait| self.conv_poly_trait_ref_dyn(env, poly_trait))
.try_collect()?;
let region = self.conv_lifetime(env, *lft);
let kind = Self::conv_trait_object_syntax(syn);
Ok(rty::Ty::dynamic(exi_preds, region, kind))
}
}
}

fn conv_trait_object_syntax(syn: &rustc_ast::TraitObjectSyntax) -> DynKind {
match syn {
rustc_ast::TraitObjectSyntax::Dyn => DynKind::Dyn,
rustc_ast::TraitObjectSyntax::None => DynKind::None,
rustc_ast::TraitObjectSyntax::DynStar => {
tracked_span_bug!("dyn* traits not supported yet")
if matches!(syn, rustc_ast::TraitObjectSyntax::Dyn) {
Ok(rty::Ty::dynamic(exi_preds, region))
} else {
tracked_span_bug!("dyn* traits not supported yet")
ranjitjhala marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/flux-middle/src/rty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 971,7 @@ impl TypeSuperVisitable for BaseTy {
| BaseTy::Closure(_, _)
| BaseTy::Never
| BaseTy::Param(_) => ControlFlow::Continue(()),
BaseTy::Dynamic(exi_preds, _, _) => exi_preds.visit_with(visitor),
BaseTy::Dynamic(exi_preds, _) => exi_preds.visit_with(visitor),
}
}
}
Expand Down Expand Up @@ -1011,12 1011,8 @@ impl TypeSuperFoldable for BaseTy {
args.try_fold_with(folder)?,
)
}
BaseTy::Dynamic(exi_preds, region, syn) => {
BaseTy::Dynamic(
exi_preds.try_fold_with(folder)?,
region.try_fold_with(folder)?,
*syn,
)
BaseTy::Dynamic(exi_preds, region) => {
BaseTy::Dynamic(exi_preds.try_fold_with(folder)?, region.try_fold_with(folder)?)
}
};
Ok(bty)
Expand Down
27 changes: 13 additions & 14 deletions crates/flux-middle/src/rty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 57,7 @@ use crate::{
intern::{impl_internable, impl_slice_internable, Interned, List},
queries::QueryResult,
rty::subst::SortSubst,
rustc::{
self,
mir::Place,
ty::{DynKind, VariantDef},
},
rustc::{self, mir::Place, ty::VariantDef},
};

#[derive(Debug, Clone, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
Expand Down Expand Up @@ -642,12 638,8 @@ impl Ty {
Self::alias(AliasKind::Projection, alias_ty)
}

pub fn dynamic(
preds: impl Into<List<Binder<ExistentialPredicate>>>,
region: Region,
kind: DynKind,
) -> Ty {
BaseTy::Dynamic(preds.into(), region, kind).to_ty()
pub fn dynamic(preds: impl Into<List<Binder<ExistentialPredicate>>>, region: Region) -> Ty {
BaseTy::Dynamic(preds.into(), region).to_ty()
}

pub fn strg_ref(re: Region, path: Path, ty: Ty) -> Ty {
Expand Down Expand Up @@ -979,7 971,7 @@ pub enum BaseTy {
Never,
Closure(DefId, /* upvar_tys */ List<Ty>),
Coroutine(DefId, /*resume_ty: */ Ty, /* upvar_tys: */ List<Ty>),
Dynamic(List<Binder<ExistentialPredicate>>, Region, DynKind),
Dynamic(List<Binder<ExistentialPredicate>>, Region),
Param(ParamTy),
}

Expand Down Expand Up @@ -1128,7 1120,7 @@ impl BaseTy {
| BaseTy::Array(_, _)
| BaseTy::Closure(_, _)
| BaseTy::Coroutine(..)
| BaseTy::Dynamic(_, _, _)
| BaseTy::Dynamic(_, _)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something I don't quite understand yet. We are putting Dynamic in BaseTy which means they can be indexed (by unit as returned by this function), however, the concrete type under the dynamic type could be indexed by another sort. I don't know if these should be somehow related or if they are completely orthogonal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine, because once its Dynamic you have zero information about the underlying concrete type, everything is abstracted to whatever is know about the dyn...

| BaseTy::Never => Sort::unit(),
}
}
Expand Down Expand Up @@ -1161,7 1153,14 @@ impl BaseTy {
BaseTy::Array(_, _) => todo!(),
BaseTy::Never => tcx.types.never,
BaseTy::Closure(_, _) => todo!(),
BaseTy::Dynamic(_, _, _) => todo!(),
BaseTy::Dynamic(_exi_preds, _re) => {
todo!()
// let preds = exi_preds
// .iter()
// .map(|pred| pred.to_rustc(tcx))
// .collect_vec();
// ty::Ty::new_dynamic(tcx, preds, re.to_rustc(tcx), rustc_middle::ty::DynKind::Dyn)
}
BaseTy::Coroutine(def_id, resume_ty, upvars) => {
todo!("Generator {def_id:?} {resume_ty:?} {upvars:?}")
// let args = args.iter().map(|arg| into_rustc_generic_arg(tcx, arg));
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-middle/src/rty/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 399,7 @@ impl Pretty for BaseTy {
}
Ok(())
}
BaseTy::Dynamic(exi_preds, _, _) => {
BaseTy::Dynamic(exi_preds, _) => {
w!("dyn {:?}", join!(", ", exi_preds))
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/flux-middle/src/rty/refining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 382,12 @@ impl<'genv, 'tcx> Refiner<'genv, 'tcx> {
rustc::ty::TyKind::RawPtr(ty, mu) => {
rty::BaseTy::RawPtr(self.as_default().refine_ty(ty)?, *mu)
}
rustc::ty::TyKind::Dynamic(exi_preds, r, kind) => {
rustc::ty::TyKind::Dynamic(exi_preds, r) => {
let exi_preds = exi_preds
.iter()
.map(|ty| self.refine_existential_predicate(ty))
.try_collect()?;
rty::BaseTy::Dynamic(exi_preds, *r, *kind)
rty::BaseTy::Dynamic(exi_preds, *r)
}
};
Ok(TyOrBase::Base((self.refine)(bty)))
Expand Down
28 changes: 14 additions & 14 deletions crates/flux-middle/src/rustc/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 25,9 @@ use super::{
},
ty::{
AdtDef, AdtDefData, AliasKind, Binder, BoundRegion, BoundVariableKind, Clause, ClauseKind,
Const, ConstKind, DynKind, ExistentialPredicate, FieldDef, FnSig, GenericArg,
GenericParamDef, GenericParamDefKind, GenericPredicates, Generics, OutlivesPredicate,
PolyFnSig, TraitPredicate, TraitRef, Ty, TypeOutlivesPredicate, VariantDef,
Const, ConstKind, ExistentialPredicate, FieldDef, FnSig, GenericArg, GenericParamDef,
GenericParamDefKind, GenericPredicates, Generics, OutlivesPredicate, PolyFnSig,
TraitPredicate, TraitRef, Ty, TypeOutlivesPredicate, VariantDef,
},
};
use crate::{
Expand Down Expand Up @@ -740,17 740,17 @@ pub(crate) fn lower_ty<'tcx>(
let args = lower_generic_args(tcx, args)?;
Ok(Ty::mk_generator_witness(*did, args))
}
rustc_ty::Dynamic(predicates, region, kind) => {
rustc_ty::Dynamic(predicates, region, rustc_ty::DynKind::Dyn) => {
let region = lower_region(region)?;
let kind = lower_dyn_kind(kind)?;

let exi_preds = List::from_vec(
predicates
.iter()
.map(|pred| lower_existential_predicate(tcx, pred))
.try_collect()?,
);

Ok(Ty::mk_dynamic(exi_preds, region, kind))
Ok(Ty::mk_dynamic(exi_preds, region))
}
_ => Err(UnsupportedReason::new(format!("unsupported type `{ty:?}`"))),
}
Expand Down Expand Up @@ -822,14 822,14 @@ fn lower_generic_arg<'tcx>(
}
}

fn lower_dyn_kind(kind: &rustc_ty::DynKind) -> Result<DynKind, UnsupportedReason> {
match kind {
rustc_ty::DynKind::Dyn => Ok(DynKind::Dyn),
rustc_ty::DynKind::DynStar => {
Err(UnsupportedReason::new(format!("unsupported dyn kind `{kind:?}`")))
}
}
}
// fn lower_dyn_kind(kind: &rustc_ty::DynKind) -> Result<DynKind, UnsupportedReason> {
// match kind {
// rustc_ty::DynKind::Dyn => Ok(DynKind::Dyn),
// rustc_ty::DynKind::DynStar => {
// Err(UnsupportedReason::new(format!("unsupported dyn kind `{kind:?}`")))
// }
// }
// }

fn lower_region(region: &rustc_middle::ty::Region) -> Result<Region, UnsupportedReason> {
use rustc_middle::ty::RegionKind;
Expand Down
20 changes: 5 additions & 15 deletions crates/flux-middle/src/rustc/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 182,7 @@ pub enum TyKind {
CoroutineWitness(DefId, GenericArgs),
Alias(AliasKind, AliasTy),
RawPtr(Ty, Mutability),
Dynamic(List<Binder<ExistentialPredicate>>, Region, DynKind),
Dynamic(List<Binder<ExistentialPredicate>>, Region),
}

#[derive(Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
Expand All @@ -196,12 196,6 @@ pub struct ExistentialTraitRef {
pub args: GenericArgs,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
pub enum DynKind {
Dyn,
None,
}

#[derive(Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
pub struct AliasTy {
pub args: GenericArgs,
Expand Down Expand Up @@ -648,12 642,8 @@ impl Ty {
TyKind::Param(param).intern()
}

pub fn mk_dynamic(
exi_preds: impl Into<List<Binder<ExistentialPredicate>>>,
r: Region,
kind: DynKind,
) -> Ty {
TyKind::Dynamic(exi_preds.into(), r, kind).intern()
pub fn mk_dynamic(exi_preds: impl Into<List<Binder<ExistentialPredicate>>>, r: Region) -> Ty {
TyKind::Dynamic(exi_preds.into(), r).intern()
}

pub fn mk_ref(region: Region, ty: Ty, mutability: Mutability) -> Ty {
Expand Down Expand Up @@ -747,7 737,7 @@ impl Ty {
TyKind::Coroutine(_, _) => todo!(),
TyKind::CoroutineWitness(_, _) => todo!(),
TyKind::Alias(_, _) => todo!(),
TyKind::Dynamic(_, _, _) => todo!(),
TyKind::Dynamic(_, _) => todo!(),
};
rustc_ty::Ty::new(tcx, kind)
}
Expand Down Expand Up @@ -870,7 860,7 @@ impl fmt::Debug for Ty {
write!(f, ")")?;
Ok(())
}
TyKind::Dynamic(exi_preds, _r, _syn) => {
TyKind::Dynamic(exi_preds, _r) => {
write!(f, "dyn {exi_preds:?}")
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-middle/src/rustc/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,7 @@ impl Subst for Ty {
TyKind::RawPtr(ty, mutbl) => Ty::mk_raw_ptr(ty.subst(args), *mutbl),
TyKind::Param(param_ty) => args[param_ty.index as usize].expect_type().clone(),
TyKind::FnPtr(fn_sig) => Ty::mk_fn_ptr(fn_sig.subst(args)),
TyKind::Dynamic(exi_preds, re, syn) => Ty::mk_dynamic(exi_preds.subst(args), *re, *syn),
TyKind::Dynamic(exi_preds, re) => Ty::mk_dynamic(exi_preds.subst(args), *re),
TyKind::Bool
| TyKind::Uint(_)
| TyKind::Str
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-refineck/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 974,7 @@ impl<'ck, 'genv, 'tcx, M: Mode> Checker<'ck, 'genv, 'tcx, M> {
Ty::mk_ref(*dst_re, dst_slice, *dst_mut)
} else
// &T -> & dyn U
if let rustc::ty::TyKind::Dynamic(_, _, _) = dst_ty.kind() {
if let rustc::ty::TyKind::Dynamic(_, _) = dst_ty.kind() {
self.genv
.refine_default(&self.generics, to)
.with_span(self.body.span())?
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-refineck/src/constraint_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 584,7 @@ impl<'a, 'genv, 'tcx> InferCtxt<'a, 'genv, 'tcx> {
| (BaseTy::Str, BaseTy::Str)
| (BaseTy::Char, BaseTy::Char)
| (BaseTy::RawPtr(_, _), BaseTy::RawPtr(_, _))
| (BaseTy::Dynamic(_, _, _), BaseTy::Dynamic(_, _, _)) => Ok(()),
| (BaseTy::Dynamic(_, _), BaseTy::Dynamic(_, _)) => Ok(()),
ranjitjhala marked this conversation as resolved.
Show resolved Hide resolved
(BaseTy::Closure(did1, tys1), BaseTy::Closure(did2, tys2)) if did1 == did2 => {
debug_assert_eq!(tys1.len(), tys2.len());
for (ty1, ty2) in iter::zip(tys1, tys2) {
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-refineck/src/type_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 373,7 @@ impl BasicBlockEnvShape {
| BaseTy::Char
| BaseTy::Never
| BaseTy::Closure(_, _)
| BaseTy::Dynamic(_, _, _)
| BaseTy::Dynamic(_, _)
| BaseTy::Coroutine(..) => bty.clone(),
}
}
Expand Down
Loading