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

Improve inline syntax macro #562

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion crates/flux-desugar/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ impl<'a, 'tcx> RustItemCtxt<'a, 'tcx> {
})
.try_collect_exhaust()?;

let ret = self.desugar_variant_ret(&variant_def.ret, &mut env)?;
let ret = if let Some(ret) = &variant_def.ret {
self.desugar_variant_ret(ret, &mut env)?
} else {
self.as_lift_cx().lift_variant_ret()
};

Ok(fhir::VariantDef {
def_id: hir_variant.def_id,
Expand Down
4 changes: 3 additions & 1 deletion crates/flux-desugar/src/desugar/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ impl RustItemCtxt<'_, '_> {
self.gather_params_ty(None, ty, TypePos::Input, &mut env)?;
}

self.gather_params_variant_ret(&variant_def.ret, &mut env)?;
if let Some(ret) = &variant_def.ret {
self.gather_params_variant_ret(ret, &mut env)?;
}

self.check_param_uses(&mut env, |vis| vis.visit_variant(variant_def))?;

Expand Down
4 changes: 3 additions & 1 deletion crates/flux-desugar/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ impl<'a> ItemLikeResolver<'a> {
.fields
.iter()
.try_for_each_exhaust(|ty| self.resolve_ty(ty))?;
self.resolve_variant_ret(&variant_def.ret)?;
if let Some(ret) = &variant_def.ret {
self.resolve_variant_ret(ret)?;
}
}
Ok(())
}
Expand Down
40 changes: 26 additions & 14 deletions crates/flux-middle/src/fhir/lift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,7 @@ impl<'a, 'tcx> LiftCtxt<'a, 'tcx> {
variant: &hir::Variant,
) -> Result<fhir::VariantDef, ErrorGuaranteed> {
let item = self.tcx.hir().expect_item(self.owner.def_id);
let hir::ItemKind::Enum(_, generics) = &item.kind else {
bug!("expected an enum or struct")
};
let hir::ItemKind::Enum(_, generics) = &item.kind else { bug!("expected an enum") };

let fields = variant
.data
Expand All @@ -375,6 +373,29 @@ impl<'a, 'tcx> LiftCtxt<'a, 'tcx> {
.map(|field| self.lift_field_def(field))
.try_collect_exhaust()?;

let ret = self.lift_variant_ret_inner(item, generics);

Ok(fhir::VariantDef {
def_id: variant.def_id,
params: vec![],
fields,
ret,
span: variant.span,
lifted: true,
})
}

pub fn lift_variant_ret(&mut self) -> fhir::VariantRet {
let item = self.tcx.hir().expect_item(self.owner.def_id);
let hir::ItemKind::Enum(_, generics) = &item.kind else { bug!("expected an enum") };
self.lift_variant_ret_inner(item, generics)
}

fn lift_variant_ret_inner(
&mut self,
item: &hir::Item,
generics: &hir::Generics,
) -> fhir::VariantRet {
let span = item.ident.span.to(generics.span);
let path = fhir::Path {
res: fhir::Res::SelfTyAlias { alias_to: self.owner.to_def_id(), is_trait_impl: false },
Expand All @@ -384,24 +405,15 @@ impl<'a, 'tcx> LiftCtxt<'a, 'tcx> {
span,
};
let bty = fhir::BaseTy::from(fhir::QPath::Resolved(None, path));
let ret = fhir::VariantRet {
fhir::VariantRet {
bty,
idx: fhir::RefineArg::Record(
self.owner.to_def_id(),
List::empty(), // TODO:RJ: or should we use the generics and just make it T1,...Tn?
vec![],
generics.span.shrink_to_hi(),
),
};

Ok(fhir::VariantDef {
def_id: variant.def_id,
params: vec![],
fields,
ret,
span: variant.span,
lifted: true,
})
}
}

fn lift_ty(&mut self, ty: &hir::Ty) -> Result<fhir::Ty, ErrorGuaranteed> {
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-syntax/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Async: surface::Async = {
}

pub Variant: surface::VariantDef = {
<lo:@L> <tys:Fields?> <ret:VariantRet> <hi:@R> => {
<lo:@L> <tys:Fields?> <ret:VariantRet?> <hi:@R> => {
let fields = match tys {
Some(fields) => fields,
None => vec![],
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-syntax/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl EnumDef {
#[derive(Debug)]
pub struct VariantDef {
pub fields: Vec<Ty>,
pub ret: VariantRet,
pub ret: Option<VariantRet>,
pub node_id: NodeId,
pub span: Span,
}
Expand Down
4 changes: 3 additions & 1 deletion crates/flux-syntax/src/surface/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ pub fn walk_enum_def<V: Visitor>(vis: &mut V, enum_def: &EnumDef) {

pub fn walk_variant<V: Visitor>(vis: &mut V, variant: &VariantDef) {
walk_list!(vis, visit_ty, &variant.fields);
vis.visit_variant_ret(&variant.ret);
if let Some(ret) = &variant.ret {
vis.visit_variant_ret(ret);
}
}

pub fn walk_variant_ret<V: Visitor>(vis: &mut V, ret: &VariantRet) {
Expand Down
Loading
Loading