|
| 1 | +use std::iter; |
| 2 | + |
| 3 | +use prusti_rustc_interface::{middle::ty::AssocKind, span::def_id::DefId}; |
| 4 | +use task_encoder::{EncodeFullResult, TaskEncoder, TaskEncoderDependencies}; |
| 5 | +use vir::{CastType, Domain, vir_format_identifier}; |
| 6 | + |
| 7 | +use crate::encoders::ty::{ |
| 8 | + RustTyDecomposition, |
| 9 | + generics::{GArgs, GArgsTyEnc, GParams, GenericParamsEnc, traits::TraitEnc}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct TraitImplEnc; |
| 13 | + |
| 14 | +impl TaskEncoder for TraitImplEnc { |
| 15 | + task_encoder::encoder_cache!(TraitImplEnc); |
| 16 | + |
| 17 | + fn task_to_key<'vir>(task: &Self::TaskDescription<'vir>) -> Self::TaskKey<'vir> { |
| 18 | + *task |
| 19 | + } |
| 20 | + |
| 21 | + fn emit_outputs<'vir>(program: &mut task_encoder::Program<'vir>) { |
| 22 | + for dom in TraitImplEnc::all_outputs_local_no_errors() { |
| 23 | + program.add_domain(dom); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + type TaskDescription<'vir> = DefId; |
| 28 | + type OutputFullLocal<'vir> = Domain<'vir>; |
| 29 | + |
| 30 | + fn do_encode_full<'vir>( |
| 31 | + task_key: &Self::TaskKey<'vir>, |
| 32 | + deps: &mut TaskEncoderDependencies<'vir, Self>, |
| 33 | + ) -> EncodeFullResult<'vir, Self> { |
| 34 | + deps.emit_output_ref(*task_key, ())?; |
| 35 | + |
| 36 | + vir::with_vcx(|vcx| { |
| 37 | + let tcx = vcx.tcx(); |
| 38 | + |
| 39 | + let ctx = GParams::from(*task_key); |
| 40 | + |
| 41 | + let params = deps.require_dep::<GenericParamsEnc>(ctx)?; |
| 42 | + |
| 43 | + let trait_ref = tcx.impl_trait_ref(task_key).unwrap().instantiate_identity(); |
| 44 | + let trait_did = trait_ref.def_id; |
| 45 | + let trait_data = deps.require_dep::<TraitEnc>(trait_did)?; |
| 46 | + |
| 47 | + let args = deps.require_dep::<GArgsTyEnc>(GArgs::new(ctx, trait_ref.args))?; |
| 48 | + |
| 49 | + let mut axs = Vec::new(); |
| 50 | + |
| 51 | + let struct_ty = tcx.type_of(task_key).instantiate_identity(); |
| 52 | + |
| 53 | + let impl_fun = trait_data.impl_fun; |
| 54 | + let trait_ty_decls = params |
| 55 | + .ty_decls() |
| 56 | + .iter() |
| 57 | + .map(|dec| dec.upcast_ty()) |
| 58 | + .collect::<Vec<_>>(); |
| 59 | + let trait_tys = args.get_ty(); |
| 60 | + |
| 61 | + axs.push( |
| 62 | + vcx.mk_domain_axiom( |
| 63 | + vir_format_identifier!(vcx, "{}_impl_{}", trait_data.trait_name, struct_ty), |
| 64 | + vir::expr! {forall ..[trait_ty_decls] :: {[impl_fun(trait_tys)]} [impl_fun(trait_tys)]} |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + tcx.associated_items(*task_key) |
| 69 | + .in_definition_order() |
| 70 | + .filter(|item| matches!(item.kind, AssocKind::Type { data: _ })) |
| 71 | + .for_each(|impl_item| { |
| 72 | + let assoc_fun = trait_data.type_did_fun_mapping.get(&impl_item.trait_item_def_id.unwrap()).unwrap(); |
| 73 | + // construct arguments for assoc_item function |
| 74 | + // parameters of the trait are substituted |
| 75 | + // by the arguments used in the impl |
| 76 | + // parameters of the associated type are kept |
| 77 | + |
| 78 | + // parameters of assoc item include already substituted arguments |
| 79 | + let assoc_params = deps |
| 80 | + .require_dep::<GenericParamsEnc>(GParams::from(impl_item.def_id)) |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + // the type we want to resolve the type alias to |
| 84 | + let assoc_type_expr = assoc_params.ty_expr( |
| 85 | + deps, |
| 86 | + RustTyDecomposition::from_ty( |
| 87 | + tcx.type_of(impl_item.def_id).instantiate_identity(), |
| 88 | + tcx, |
| 89 | + GParams::from(impl_item.def_id), |
| 90 | + ), |
| 91 | + ); |
| 92 | + let assoc_decls = assoc_params |
| 93 | + .ty_decls() |
| 94 | + .iter() |
| 95 | + .map(|dec| dec.upcast_ty()) |
| 96 | + .collect::<Vec<_>>(); |
| 97 | + |
| 98 | + // Combine substituted trait ty decls with the decls of the associated type |
| 99 | + let mut trait_ty_decls = trait_ty_decls.clone(); |
| 100 | + trait_ty_decls.extend_from_slice(&assoc_decls[params.ty_exprs().len()..]); |
| 101 | + |
| 102 | + // Combine substituted trait params with the params of the associated type |
| 103 | + let trait_tys = vcx.alloc_slice(&iter::empty().chain(args.get_ty().to_owned()).chain(assoc_params.ty_exprs()[params.ty_exprs().len()..].to_owned()).collect::<Vec<_>>()); |
| 104 | + axs.push(vcx.mk_domain_axiom( |
| 105 | + vir_format_identifier!( |
| 106 | + vcx, |
| 107 | + "{}_Assoc_{}_{}", |
| 108 | + trait_data.trait_name, |
| 109 | + tcx.item_name(impl_item.def_id), |
| 110 | + struct_ty |
| 111 | + ), |
| 112 | + vir::expr! {forall ..[trait_ty_decls] :: {[assoc_fun(trait_tys)]} ([assoc_fun(trait_tys)]) == (assoc_type_expr)} |
| 113 | + )); |
| 114 | + }); |
| 115 | + |
| 116 | + Ok(( |
| 117 | + vcx.mk_domain( |
| 118 | + vir_format_identifier!( |
| 119 | + vcx, |
| 120 | + "t_{}_{}", |
| 121 | + trait_data.trait_name, |
| 122 | + tcx.type_of(*task_key).instantiate_identity().to_string() |
| 123 | + ), |
| 124 | + &[], |
| 125 | + vcx.alloc_slice(&axs), |
| 126 | + &[], |
| 127 | + None, |
| 128 | + ), |
| 129 | + (), |
| 130 | + )) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments