Skip to content

Commit

Permalink
remove sort_params from RefinedBy; find it from Sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranjit Jhala committed Oct 20, 2023
1 parent c1e2c89 commit 90aec9f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 30 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ For an overview, take a look at the [`flux` website](https://flux-rs.github.io).

Documentation, including installation and usage guides can be found on the
[website](https://flux-rs.github.io/flux).

-----
fn sort_params(
generics: &rustc_middle::ty::Generics,
refined_by: &surface::RefinedBy,
) -> Vec<DefId> {
let sort_params: FxHashSet<_> = refined_by
.sort_params
.iter()
.map(|ident| ident.name)
.collect();
let mut params = vec![];
for param in &generics.params {
if let rustc_middle::ty::GenericParamDefKind::Type { .. } = param.kind &&
sort_params.contains(&param.name)
{
params.push(param.def_id);
}
}
params
}
5 changes: 2 additions & 3 deletions crates/flux-desugar/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_hir as hir;
use rustc_hir::OwnerId;
use rustc_middle::ty::Generics;
use rustc_span::{
def_id::{DefId, LocalDefId},
def_id::LocalDefId,
sym::{self},
symbol::kw,
Span, Symbol,
Expand Down Expand Up @@ -108,10 +108,9 @@ pub fn desugar_refined_by(
))
})
.try_collect_exhaust()?;
let sort_params: Vec<_> = sort_params(generics, refined_by);
Ok(fhir::RefinedBy::new(
owner_id.def_id,
sort_params,
&generics,
early_bound_params,
index_params,
refined_by.span,
Expand Down
46 changes: 35 additions & 11 deletions crates/flux-middle/src/fhir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ impl Generics {
impl RefinedBy {
pub fn new(
def_id: impl Into<DefId>,
generics: &Generics,
generics: &rustc_middle::ty::Generics,
early_bound_params: impl IntoIterator<Item = Sort>,
index_params: impl IntoIterator<Item = (Symbol, Sort)>,
span: Span,
Expand All @@ -800,25 +800,21 @@ impl RefinedBy {
.into_iter()
.inspect(|(_, sort)| sorts.push(sort.clone()))
.collect();
// let sort_params = sort_params.into_iter().collect();
let sort_params = Self::sort_params(generics, &sorts);
RefinedBy { def_id: def_id.into(), sort_params, span, index_params, early_bound, sorts }
}

fn sort_params(generics: &Generics, sorts: &Vec<Sort>) -> Vec<DefId> {
// let sort_params: FxHashSet<_> = refined_by
// .sort_params
// .iter()
// .map(|ident| ident.name)
// .collect();
fn sort_params(generics: &rustc_middle::ty::Generics, sorts: &Vec<Sort>) -> Vec<DefId> {
let mut sort_params: FxHashSet<DefId> = Default::default();

TODO_walk_over_sorts_to_gather_all_Param_defids();
for sort in sorts {
sort.gather_sort_params(&mut sort_params);
}

let mut params = vec![];
for param in &generics.params {
let def_id = param.def_id.to_def_id();
if let GenericParamKind::Type { .. } = param.kind &&
let def_id = param.def_id;
if let rustc_middle::ty::GenericParamDefKind::Type { .. } = param.kind &&
sort_params.contains(&def_id)
{
params.push(def_id);
Expand Down Expand Up @@ -954,6 +950,34 @@ impl Sort {
Sort::Func(fsort) => Sort::Func(fsort.param_subst(subst)), // bug!("unexpected subst in (nested) func-sort {self:?}"),
}
}

pub fn gather_sort_params(&self, params: &mut FxHashSet<DefId>) {
match self {
Sort::Int
| Sort::Bool
| Sort::Real
| Sort::Loc
| Sort::Unit
| Sort::BitVec(_)
| Sort::Var(_)
| Sort::Infer(_)
| Sort::Wildcard => {}
Sort::Param(def_id) => {
params.insert(*def_id);
}
Sort::App(_, args) => {
for arg in args {
arg.gather_sort_params(params);
}
}
Sort::Func(fsort) => {
for arg in &fsort.fsort.inputs_and_output {
arg.gather_sort_params(params);
}
}
Sort::Record(_, _) => bug!("unexpected record sort"),
}
}
}

impl ena::unify::UnifyKey for SortVid {
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-middle/src/fhir/lift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct LiftCtxt<'a, 'tcx> {
owner: OwnerId,
}

pub fn lift_refined_by(tcx: TyCtxt, sess: &FluxSession, owner_id: OwnerId) -> fhir::RefinedBy {
pub fn lift_refined_by(tcx: TyCtxt, owner_id: OwnerId) -> fhir::RefinedBy {
let def_id = owner_id.def_id;
let item = tcx.hir().expect_item(def_id);
match item.kind {
Expand Down
8 changes: 0 additions & 8 deletions crates/flux-syntax/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub TyAlias: surface::TyAlias = {
<ty:Ty>
<hi:@R> => {
let refined_by = surface::RefinedBy {
sort_params: vec![],
early_bound_params: early_bound_params.unwrap_or_default(),
index_params: index_params.unwrap_or_default(),
span: cx.map_span(refined_by_lo, refined_by_hi)
Expand All @@ -67,14 +66,7 @@ SortParams: Vec<Ident> = {
}

pub RefinedBy: surface::RefinedBy = {
<lo:@L> "<" <sort_params:Comma<Ident>> ">" "{" <index_params:RefineParams<"!">> "}" <hi:@R> => surface::RefinedBy {
sort_params,
index_params,
early_bound_params: vec![],
span: cx.map_span(lo, hi)
},
<lo:@L> <index_params:RefineParams<"!">> <hi:@R> => surface::RefinedBy {
sort_params: vec![],
index_params,
early_bound_params: vec![],
span: cx.map_span(lo, hi)
Expand Down
1 change: 0 additions & 1 deletion crates/flux-syntax/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ pub struct VariantRet {

#[derive(Debug, Default)]
pub struct RefinedBy {
pub sort_params: Vec<Ident>,
pub early_bound_params: Vec<RefineParam>,
pub index_params: Vec<RefineParam>,
pub span: Span,
Expand Down
13 changes: 9 additions & 4 deletions crates/flux-tests/tests/neg/error_messages/wf/poly_sort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::hash::Hash;
#[flux::opaque]
// #[flux::refined_by(<Tiger> { elems: Set<Tiger> })]
#[flux::refined_by( elems: Set<Tiger> )] //~ ERROR unbound generic `Tiger`
pub struct RSet<Tiger> {
pub inner: std::collections::HashSet<Tiger>,
#[flux::refined_by(elems: Set<Tiger>)] //~ ERROR cannot find sort `Tiger`
pub struct Foo<T> {
pub inner: std::collections::HashSet<T>,
}

#[flux::opaque]
#[flux::refined_by(elems: Set)] //~ ERROR cannot find sort `Set`
pub struct Bar<T> {
pub inner: std::collections::HashSet<T>,
}
2 changes: 1 addition & 1 deletion crates/flux-tests/tests/neg/surface/rset00.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::hash::Hash;
#[flux::opaque]
#[flux::refined_by(<Tiger> { elems: Set<Tiger> })]
#[flux::refined_by(elems: Set<Tiger>)]
pub struct RSet<Tiger> {
pub inner: std::collections::HashSet<Tiger>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-tests/tests/pos/surface/rset00.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::hash::Hash;
#[flux::opaque]
#[flux::refined_by(<Tiger> { elems: Set<Tiger> })]
#[flux::refined_by(elems: Set<Tiger> )]
pub struct RSet<Tiger> {
pub inner: std::collections::HashSet<Tiger>,
}
Expand Down

0 comments on commit 90aec9f

Please sign in to comment.