Skip to content

Commit 3f431a8

Browse files
committed
Refactor construction of type templates
1 parent 233298c commit 3f431a8

7 files changed

Lines changed: 276 additions & 307 deletions

File tree

src/analyze.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,12 @@ pub struct Analyzer<'tcx> {
123123
enum_defs: Rc<RefCell<HashMap<DefId, rty::EnumDatatypeDef>>>,
124124
}
125125

126-
impl<'tcx> crate::refine::TemplateTypeGenerator<'tcx> for Analyzer<'tcx> {
127-
fn tcx(&self) -> TyCtxt<'tcx> {
128-
self.tcx
129-
}
130-
126+
impl<'tcx> crate::refine::TemplateRegistry for Analyzer<'tcx> {
131127
fn register_template<V>(&mut self, tmpl: rty::Template<V>) -> rty::RefinedType<V> {
132128
tmpl.into_refined_type(|pred_sig| self.generate_pred_var(pred_sig))
133129
}
134130
}
135131

136-
impl<'tcx> crate::refine::UnrefinedTypeGenerator<'tcx> for Analyzer<'tcx> {
137-
fn tcx(&self) -> TyCtxt<'tcx> {
138-
self.tcx
139-
}
140-
}
141-
142132
impl<'tcx> Analyzer<'tcx> {
143133
pub fn generate_pred_var(&mut self, sig: chc::PredSig) -> chc::PredVarId {
144134
self.system

src/analyze/basic_block.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::chc;
1414
use crate::pretty::PrettyDisplayExt as _;
1515
use crate::refine::{
1616
self, Assumption, BasicBlockType, Env, PlaceType, PlaceTypeBuilder, PlaceTypeVar, TempVarIdx,
17-
TemplateTypeGenerator, UnrefinedTypeGenerator, Var,
17+
TypeBuilder, Var,
1818
};
1919
use crate::rty::{
2020
self, ClauseBuilderExt as _, ClauseScope as _, ShiftExistential as _, Subtyping as _,
@@ -222,9 +222,10 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
222222
let rty_args: IndexVec<_, _> = args
223223
.types()
224224
.map(|ty| {
225-
self.ctx
226-
.build_template_ty_with_scope(&self.env)
227-
.refined_ty(ty)
225+
TypeBuilder::new(self.tcx)
226+
.for_template(&mut self.ctx)
227+
.with_scope(&self.env)
228+
.build_refined(ty)
228229
})
229230
.collect();
230231
for (field_pty, mut variant_rty) in
@@ -237,7 +238,8 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
237238
self.ctx.extend_clauses(cs);
238239
}
239240

240-
let sort_args: Vec<_> = rty_args.iter().map(|rty| rty.ty.to_sort()).collect();
241+
let sort_args: Vec<_> =
242+
rty_args.iter().map(|rty| rty.ty.to_sort()).collect();
241243
let ty = rty::EnumType::new(ty_sym.clone(), rty_args).into();
242244

243245
let mut builder = PlaceTypeBuilder::default();
@@ -433,7 +435,10 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
433435
let func_ty = match func.const_fn_def() {
434436
// TODO: move this to well-known defs?
435437
Some((def_id, args)) if self.is_box_new(def_id) => {
436-
let inner_ty = self.ctx.build_template_ty().ty(args.type_at(0)).vacuous();
438+
let inner_ty = TypeBuilder::new(self.tcx)
439+
.for_template(&mut self.ctx)
440+
.build(args.type_at(0))
441+
.vacuous();
437442
let param = rty::RefinedType::unrefined(inner_ty.clone());
438443
let ret_term =
439444
chc::Term::box_(chc::Term::var(rty::FunctionParamIdx::from(0_usize)));
@@ -444,7 +449,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
444449
rty::FunctionType::new([param].into_iter().collect(), ret).into()
445450
}
446451
Some((def_id, args)) if self.is_mem_swap(def_id) => {
447-
let inner_ty = self.ctx.unrefined_ty(args.type_at(0)).vacuous();
452+
let inner_ty = TypeBuilder::new(self.tcx).build(args.type_at(0)).vacuous();
448453
let param1 =
449454
rty::RefinedType::unrefined(rty::PointerType::mut_to(inner_ty.clone()).into());
450455
let param2 =
@@ -531,7 +536,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
531536
}
532537

533538
fn add_prophecy_var(&mut self, statement_index: usize, ty: mir_ty::Ty<'tcx>) {
534-
let ty = self.ctx.unrefined_ty(ty);
539+
let ty = TypeBuilder::new(self.tcx).build(ty);
535540
let temp_var = self.env.push_temp_var(ty.vacuous());
536541
self.prophecy_vars.insert(statement_index, temp_var);
537542
tracing::debug!(stmt_idx = %statement_index, temp_var = ?temp_var, "add_prophecy_var");
@@ -552,7 +557,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
552557
referent: mir::Place<'tcx>,
553558
prophecy_ty: mir_ty::Ty<'tcx>,
554559
) -> rty::RefinedType<Var> {
555-
let prophecy_ty = self.ctx.unrefined_ty(prophecy_ty);
560+
let prophecy_ty = TypeBuilder::new(self.tcx).build(prophecy_ty);
556561
let prophecy = self.env.push_temp_var(prophecy_ty.vacuous());
557562
let place = self.elaborate_place_for_borrow(&referent);
558563
self.env.borrow_place(place, prophecy).into()
@@ -664,10 +669,10 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
664669
}
665670

666671
let decl = self.local_decls[destination].clone();
667-
let rty = self
668-
.ctx
669-
.build_template_ty_with_scope(&self.env)
670-
.refined_ty(decl.ty);
672+
let rty = TypeBuilder::new(self.tcx)
673+
.for_template(&mut self.ctx)
674+
.with_scope(&self.env)
675+
.build_refined(decl.ty);
671676
self.type_call(func.clone(), args.clone().into_iter().map(|a| a.node), &rty);
672677
self.bind_local(destination, rty);
673678
}
@@ -738,9 +743,10 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
738743
#[tracing::instrument(skip(self))]
739744
fn ret_template(&mut self) -> rty::RefinedType<Var> {
740745
let ret_ty = self.body.local_decls[mir::RETURN_PLACE].ty;
741-
self.ctx
742-
.build_template_ty_with_scope(&self.env)
743-
.refined_ty(ret_ty)
746+
TypeBuilder::new(self.tcx)
747+
.for_template(&mut self.ctx)
748+
.with_scope(&self.env)
749+
.build_refined(ret_ty)
744750
}
745751

746752
// TODO: remove this

src/analyze/crate_.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::symbol::Ident;
1111
use crate::analyze;
1212
use crate::annot::{self, AnnotFormula, AnnotParser, ResolverExt as _};
1313
use crate::chc;
14-
use crate::refine::{self, TemplateTypeGenerator, UnrefinedTypeGenerator};
14+
use crate::refine::{self, TypeBuilder};
1515
use crate::rty::{self, ClauseBuilderExt as _};
1616

1717
/// An implementation of local crate analysis.
@@ -132,13 +132,13 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
132132

133133
let mut param_resolver = analyze::annot::ParamResolver::default();
134134
for (input_ident, input_ty) in self.tcx.fn_arg_names(def_id).iter().zip(sig.inputs()) {
135-
let input_ty = self.ctx.unrefined_ty(*input_ty);
135+
let input_ty = TypeBuilder::new(self.tcx).build(*input_ty);
136136
param_resolver.push_param(input_ident.name, input_ty.to_sort());
137137
}
138138

139139
let mut require_annot = self.extract_require_annot(&param_resolver, def_id);
140140
let mut ensure_annot = {
141-
let output_ty = self.ctx.unrefined_ty(sig.output());
141+
let output_ty = TypeBuilder::new(self.tcx).build(sig.output());
142142
let resolver = annot::StackedResolver::default()
143143
.resolver(analyze::annot::ResultResolver::new(output_ty.to_sort()))
144144
.resolver((&param_resolver).map(rty::RefinedTypeVar::Free));
@@ -175,7 +175,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
175175
self.trusted.insert(def_id);
176176
}
177177

178-
let mut builder = self.ctx.build_function_template_ty(sig);
178+
let mut builder = TypeBuilder::new(self.tcx).for_function_template(&mut self.ctx, sig);
179179
if let Some(AnnotFormula::Formula(require)) = require_annot {
180180
let formula = require.map_var(|idx| {
181181
if idx.index() == sig.inputs().len() - 1 {
@@ -303,8 +303,9 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
303303

304304
// the subst doesn't contain refinements, so it's OK to take ty only
305305
// after substitution
306-
let mut field_rty =
307-
rty::RefinedType::unrefined(self.ctx.unrefined_ty(field_ty));
306+
let mut field_rty = rty::RefinedType::unrefined(
307+
TypeBuilder::new(self.tcx).build(field_ty),
308+
);
308309
field_rty.subst_ty_params(&subst);
309310
field_rty.ty
310311
})

src/analyze/local_def.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::def_id::LocalDefId;
1111
use crate::analyze;
1212
use crate::chc;
1313
use crate::pretty::PrettyDisplayExt as _;
14-
use crate::refine::{BasicBlockType, TemplateTypeGenerator};
14+
use crate::refine::{BasicBlockType, TypeBuilder};
1515
use crate::rty;
1616

1717
/// An implementation of the typing of local definitions.
@@ -306,7 +306,9 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
306306
}
307307
// function return type is basic block return type
308308
let ret_ty = self.body.local_decls[mir::RETURN_PLACE].ty;
309-
let rty = self.ctx.basic_block_template_ty(live_locals, ret_ty);
309+
let rty = TypeBuilder::new(self.tcx)
310+
.for_template(&mut self.ctx)
311+
.build_basic_block(live_locals, ret_ty);
310312
self.ctx.register_basic_block_ty(self.local_def_id, bb, rty);
311313
}
312314
}

src/refine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! module and remove this one.
99
1010
mod template;
11-
pub use template::{TemplateScope, TemplateTypeGenerator, UnrefinedTypeGenerator};
11+
pub use template::{TemplateRegistry, TemplateScope, TypeBuilder};
1212

1313
mod basic_block;
1414
pub use basic_block::BasicBlockType;

src/refine/env.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ impl rty::ClauseScope for Env {
558558
}
559559
}
560560

561-
impl refine::TemplateScope<Var> for Env {
561+
impl refine::TemplateScope for Env {
562+
type Var = Var;
562563
fn build_template(&self) -> rty::TemplateBuilder<Var> {
563564
let mut builder = rty::TemplateBuilder::default();
564565
for (v, sort) in self.dependencies() {

0 commit comments

Comments
 (0)