forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 5
Adding support for type aliases to associated types #120
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
Merged
Aurel300
merged 26 commits into
Aurel300:rewrite-2023
from
ThomasMayerl:type_alias_assoc_type
Dec 16, 2025
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9a6e5f1
Add support for type aliases with associated types
ThomasMayerl 2b77629
Run clippy
ThomasMayerl cfc09fd
Solve clippy issue
ThomasMayerl f1aee2a
Fix issue with associated types for primitive implementations
ThomasMayerl f5c131e
Move generation of trait/impl into its own encoder
ThomasMayerl 24c66d7
Run clippy --fix
ThomasMayerl 5ed73d9
Fix issue that trait/impl encodings use invalid identifiers
ThomasMayerl 0fd9e87
Run clippy --fix
ThomasMayerl 7a743e6
Add impl function for traits
ThomasMayerl cfaeef0
fix issues
ThomasMayerl fd627e4
Support generic traits
ThomasMayerl 1d36e9d
Run clippy
ThomasMayerl 1f10be8
Make trait and impl support different number of parameters
ThomasMayerl eb9ff51
Adapt test file
ThomasMayerl 352b4ff
Adapt test file
ThomasMayerl d588579
Fix cycle issue for simple test cases
ThomasMayerl 1f5b71a
Allow associated types to be generic
ThomasMayerl 970de05
Run clippy
0ec6d6f
Generate domains for all implementations of traits (even for primitiv…
ThomasMayerl 060201a
Run fmt on tests
ThomasMayerl 81885ab
Implement PR feedback
ThomasMayerl 0b21b46
use iterator chain instead of vec
ThomasMayerl d71b2a2
Implement feedback from PR
ThomasMayerl 3e1b4b4
Use HashMap instead of slice of pairs
ThomasMayerl 681da9f
Implement feedback from PR
ThomasMayerl 20948c9
Fix indentation
ThomasMayerl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| use std::iter; | ||
|
|
||
| use prusti_rustc_interface::{middle::ty::AssocKind, span::def_id::DefId}; | ||
| use task_encoder::{EncodeFullResult, TaskEncoder, TaskEncoderDependencies}; | ||
| use vir::{CastType, Domain, vir_format_identifier}; | ||
|
|
||
| use crate::encoders::ty::{ | ||
| RustTyDecomposition, | ||
| generics::{GArgs, GArgsTyEnc, GParams, GenericParamsEnc, traits::TraitEnc}, | ||
| }; | ||
|
|
||
| pub struct TraitImplEnc; | ||
|
|
||
| impl TaskEncoder for TraitImplEnc { | ||
| task_encoder::encoder_cache!(TraitImplEnc); | ||
|
|
||
| fn task_to_key<'vir>(task: &Self::TaskDescription<'vir>) -> Self::TaskKey<'vir> { | ||
| *task | ||
| } | ||
|
|
||
| fn emit_outputs<'vir>(program: &mut task_encoder::Program<'vir>) { | ||
| for dom in TraitImplEnc::all_outputs_local_no_errors() { | ||
| program.add_domain(dom); | ||
| } | ||
| } | ||
|
|
||
| type TaskDescription<'vir> = DefId; | ||
| type OutputFullLocal<'vir> = Domain<'vir>; | ||
|
|
||
| fn do_encode_full<'vir>( | ||
| task_key: &Self::TaskKey<'vir>, | ||
| deps: &mut TaskEncoderDependencies<'vir, Self>, | ||
| ) -> EncodeFullResult<'vir, Self> { | ||
| deps.emit_output_ref(*task_key, ())?; | ||
|
|
||
| vir::with_vcx(|vcx| { | ||
| let tcx = vcx.tcx(); | ||
|
|
||
| let ctx = GParams::from(*task_key); | ||
|
|
||
| let params = deps.require_dep::<GenericParamsEnc>(ctx)?; | ||
|
|
||
| let trait_ref = tcx.impl_trait_ref(task_key).unwrap().instantiate_identity(); | ||
| let trait_did = trait_ref.def_id; | ||
| let trait_data = deps.require_dep::<TraitEnc>(trait_did)?; | ||
|
|
||
| let args = deps.require_dep::<GArgsTyEnc>(GArgs::new(ctx, trait_ref.args))?; | ||
|
|
||
| let mut axs = Vec::new(); | ||
|
|
||
| let struct_ty = tcx.type_of(task_key).instantiate_identity(); | ||
|
|
||
| let impl_fun = trait_data.impl_fun; | ||
| let trait_ty_decls = params | ||
| .ty_decls() | ||
| .iter() | ||
| .map(|dec| dec.upcast_ty()) | ||
| .collect::<Vec<_>>(); | ||
| let trait_tys = args.get_ty(); | ||
|
|
||
| axs.push( | ||
| vcx.mk_domain_axiom( | ||
| vir_format_identifier!(vcx, "{}_impl_{}", trait_data.trait_name, struct_ty), | ||
| vir::expr! {forall ..[trait_ty_decls] :: {[impl_fun(trait_tys)]} [impl_fun(trait_tys)]} | ||
| ) | ||
| ); | ||
|
|
||
| tcx.associated_items(*task_key) | ||
| .in_definition_order() | ||
| .filter(|item| matches!(item.kind, AssocKind::Type { data: _ })) | ||
| .for_each(|impl_item| { | ||
| let assoc_fun = trait_data.type_did_fun_mapping.get(&impl_item.trait_item_def_id.unwrap()).unwrap(); | ||
| // construct arguments for assoc_item function | ||
| // parameters of the trait are substituted | ||
| // by the arguments used in the impl | ||
| // parameters of the associated type are kept | ||
|
|
||
| // parameters of assoc item include already substituted arguments | ||
| let assoc_params = deps | ||
| .require_dep::<GenericParamsEnc>(GParams::from(impl_item.def_id)) | ||
| .unwrap(); | ||
|
|
||
| // the type we want to resolve the type alias to | ||
| let assoc_type_expr = assoc_params.ty_expr( | ||
| deps, | ||
| RustTyDecomposition::from_ty( | ||
| tcx.type_of(impl_item.def_id).instantiate_identity(), | ||
| tcx, | ||
| GParams::from(impl_item.def_id), | ||
| ), | ||
| ); | ||
| let assoc_decls = assoc_params | ||
| .ty_decls() | ||
| .iter() | ||
| .map(|dec| dec.upcast_ty()) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Combine substituted trait ty decls with the decls of the associated type | ||
| let mut trait_ty_decls = trait_ty_decls.clone(); | ||
| trait_ty_decls.extend_from_slice(&assoc_decls[params.ty_exprs().len()..]); | ||
|
|
||
| // Combine substituted trait params with the params of the associated type | ||
| 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<_>>()); | ||
| axs.push(vcx.mk_domain_axiom( | ||
| vir_format_identifier!( | ||
| vcx, | ||
| "{}_Assoc_{}_{}", | ||
| trait_data.trait_name, | ||
| tcx.item_name(impl_item.def_id), | ||
| struct_ty | ||
| ), | ||
| vir::expr! {forall ..[trait_ty_decls] :: {[assoc_fun(trait_tys)]} ([assoc_fun(trait_tys)]) == (assoc_type_expr)} | ||
| )); | ||
| }); | ||
|
|
||
| Ok(( | ||
| vcx.mk_domain( | ||
| vir_format_identifier!( | ||
| vcx, | ||
| "t_{}_{}", | ||
| trait_data.trait_name, | ||
| tcx.type_of(*task_key).instantiate_identity().to_string() | ||
| ), | ||
| &[], | ||
| vcx.alloc_slice(&axs), | ||
| &[], | ||
| None, | ||
| ), | ||
| (), | ||
| )) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use prusti_rustc_interface::{middle::ty::AssocKind, span::def_id::DefId}; | ||
| use task_encoder::{EncodeFullResult, TaskEncoder, TaskEncoderDependencies}; | ||
| use vir::{FunctionIdn, vir_format_identifier}; | ||
|
|
||
| use crate::encoders::ty::generics::{GParams, GenericParamsEnc}; | ||
|
|
||
| pub struct TraitEnc; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct TraitData<'vir> { | ||
| pub trait_name: &'vir str, | ||
| pub type_did_fun_mapping: HashMap<DefId, FunctionIdn<'vir, vir::ManyTyVal, vir::TyVal>>, | ||
| pub impl_fun: FunctionIdn<'vir, vir::ManyTyVal, vir::Bool>, | ||
| } | ||
|
|
||
| impl TaskEncoder for TraitEnc { | ||
| task_encoder::encoder_cache!(TraitEnc); | ||
|
|
||
| fn task_to_key<'vir>(task: &Self::TaskDescription<'vir>) -> Self::TaskKey<'vir> { | ||
| *task | ||
| } | ||
|
|
||
| type TaskDescription<'vir> = DefId; | ||
|
|
||
| type OutputFullDependency<'vir> = TraitData<'vir>; | ||
| type OutputFullLocal<'vir> = vir::Domain<'vir>; | ||
|
|
||
| fn emit_outputs<'vir>(program: &mut task_encoder::Program<'vir>) { | ||
| for dom in TraitEnc::all_outputs_local_no_errors() { | ||
| program.add_domain(dom); | ||
| } | ||
| } | ||
|
|
||
| fn do_encode_full<'vir>( | ||
| task_key: &Self::TaskKey<'vir>, | ||
| deps: &mut TaskEncoderDependencies<'vir, Self>, | ||
| ) -> EncodeFullResult<'vir, Self> { | ||
| deps.emit_output_ref(*task_key, ())?; | ||
| vir::with_vcx(|vcx| { | ||
| let tcx = vcx.tcx(); | ||
| let params = deps.require_dep::<GenericParamsEnc>(GParams::from(*task_key))?; | ||
| let trait_name = vcx.alloc_str(tcx.item_name(task_key).as_str()); | ||
| let type_did_fun_mapping = tcx | ||
| .associated_items(task_key) | ||
| .in_definition_order() | ||
| .filter(|item| matches!(item.kind, AssocKind::Type { data: _ })) | ||
| .map(|item| { | ||
| let params_type = deps | ||
| .require_dep::<GenericParamsEnc>(GParams::from(item.def_id)) | ||
| .unwrap(); | ||
| ( | ||
| item.def_id, | ||
| FunctionIdn::new( | ||
| vir_format_identifier!( | ||
| vcx, | ||
| "{}_Assoc_{}_func", | ||
| trait_name, | ||
| tcx.item_name(item.def_id), | ||
| ), | ||
| vcx.alloc_slice(&vec![vir::TYPE_TYVAL; params_type.ty_exprs().len()]), // params_type also includes parameters of trait itself | ||
| vir::TYPE_TYVAL, | ||
| ), | ||
| ) | ||
| }) | ||
| .collect::<HashMap<DefId, FunctionIdn<'vir, vir::ManyTyVal, vir::TyVal>>>(); | ||
| let mut funcs = type_did_fun_mapping | ||
| .values() | ||
| .map(|function_idn| vcx.mk_domain_function(*function_idn, false, None)) | ||
| .collect::<Vec<_>>(); | ||
| let impl_fun = FunctionIdn::new( | ||
| vir_format_identifier!(vcx, "{}_impl", trait_name), | ||
| vcx.alloc_slice(&(vec![vir::TYPE_TYVAL; params.ty_exprs().len()])), | ||
| vir::TYPE_BOOL, | ||
| ); | ||
| let impl_fun_data = vcx.mk_domain_function(impl_fun, false, None); | ||
| funcs.push(impl_fun_data); | ||
| let trait_domain = vcx.mk_domain( | ||
| vir_format_identifier!(vcx, "t_{}", trait_name), | ||
| &[], | ||
| &[], | ||
| vcx.alloc_slice(funcs.as_slice()), | ||
| None, | ||
| ); | ||
| Ok(( | ||
| trait_domain, | ||
| TraitData { | ||
| trait_name, | ||
| type_did_fun_mapping, | ||
| impl_fun, | ||
| }, | ||
| )) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ThomasMayerl marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| fn foo<Y: MyTrait>(x: Y::SomeType) {} | ||
|
|
||
| trait MyTrait { | ||
| type SomeType; | ||
| } | ||
|
|
||
| struct St1 {} | ||
| struct St2 {} | ||
|
|
||
| impl MyTrait for St1 { | ||
| type SomeType = u32; | ||
| } | ||
|
|
||
| impl MyTrait for St2 { | ||
| type SomeType = u64; | ||
| } | ||
|
|
||
| fn bar() { | ||
| foo::<St1>(5); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.