-
Notifications
You must be signed in to change notification settings - Fork 40
Support tuple in mir fn body #205
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
Draft
tiif
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
tiif:more-mir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8562c8d
Fix some typo in ValueExpression::Struct
tiif a7ccde3
Add tuple expression
tiif 5d5f350
Support field projection for tuple
tiif 561ad90
WIP: we can't get the struct parameters from adt parameters yet
tiif 2111b6c
Change how field projection works to accomodate tuple
tiif 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,7 @@ use formality_core::{judgment_fn, Fallible, Map, Upcast}; | |||||
| use formality_prove::{prove_normalize, AdtDeclBoundData, AdtDeclVariant, Constraints, Decls, Env}; | ||||||
| use formality_rust::grammar::minirust::ArgumentExpression::{ByValue, InPlace}; | ||||||
| use formality_rust::grammar::minirust::PlaceExpression::*; | ||||||
| use formality_rust::grammar::minirust::ValueExpression::{Constant, Fn, Load, Struct}; | ||||||
| use formality_rust::grammar::minirust::ValueExpression::*; | ||||||
| use formality_rust::grammar::minirust::{ | ||||||
| self, ArgumentExpression, BasicBlock, BbId, LocalId, PlaceExpression, ValueExpression, | ||||||
| }; | ||||||
|
|
@@ -281,33 +281,22 @@ impl Check<'_> { | |||||
| place_ty = ty; | ||||||
| } | ||||||
| Field(field_projection) => { | ||||||
| let ty = self.check_place(env, &field_projection.root).unwrap(); | ||||||
| let ty = self.check_place(env, &field_projection.root)?; | ||||||
|
|
||||||
| // FIXME(tiif): We eventually want to do normalization here, so check_place should be | ||||||
| // a judgment fn. | ||||||
| let Some(adt_id) = ty.get_adt_id() else { | ||||||
| bail!("The local used for field projection is not adt.") | ||||||
| }; | ||||||
|
|
||||||
| let ( | ||||||
| _, | ||||||
| AdtDeclBoundData { | ||||||
| where_clause: _, | ||||||
| variants, | ||||||
| }, | ||||||
| ) = self.decls.adt_decl(&adt_id).binder.open(); | ||||||
| let AdtDeclVariant { name, fields } = variants.last().unwrap(); | ||||||
|
|
||||||
| if *name != VariantId::for_struct() { | ||||||
| bail!("The local used for field projection must be struct.") | ||||||
| if !ty.is_tuple() && !ty.is_adt() { | ||||||
| bail!("The local used for field projection must be ADT or Tuple") | ||||||
| } | ||||||
|
|
||||||
| // Check if the index is valid for the tuple. | ||||||
| if field_projection.index >= fields.len() { | ||||||
| let parameters = ty.get_rigid_ty_parameters().unwrap(); | ||||||
|
|
||||||
| if field_projection.index >= parameters.len() { | ||||||
| bail!("The field index used in PlaceExpression::Field is invalid.") | ||||||
| } | ||||||
|
|
||||||
| place_ty = fields[field_projection.index].ty.clone(); | ||||||
| place_ty = parameters[field_projection.index].as_ty().unwrap().clone(); | ||||||
| } | ||||||
| } | ||||||
| Ok(place_ty.clone()) | ||||||
|
|
@@ -402,12 +391,12 @@ impl Check<'_> { | |||||
| bail!("This type used in ValueExpression::Struct should be a struct") | ||||||
| } | ||||||
|
|
||||||
| // Check if the number of value provided match the number of field. | ||||||
| // Check if the number of value provided matches the number of field. | ||||||
| let struct_field_tys: Vec<Ty> = | ||||||
| fields.iter().map(|field| field.ty.clone()).collect(); | ||||||
|
|
||||||
| if value_expressions.len() != struct_field_tys.len() { | ||||||
| bail!("The length of ValueExpression::Tuple does not match the type of the ADT declared") | ||||||
| bail!("The length of ValueExpression::Struct does not match the type of the ADT declared") | ||||||
| } | ||||||
|
|
||||||
| let mut value_tys: Vec<Ty> = Vec::new(); | ||||||
|
|
@@ -432,6 +421,47 @@ impl Check<'_> { | |||||
| Wcs::all_sub(value_tys, struct_field_tys), | ||||||
| )?; | ||||||
|
|
||||||
| Ok(ty.clone()) | ||||||
| } | ||||||
| Tuple(value_expressions, ty) => { | ||||||
| self.prove_goal(&typeck_env.env, &fn_assumptions, ty.well_formed())?; | ||||||
|
|
||||||
| if !ty.is_tuple() { | ||||||
| bail!("Non-tuple type provided for tuple expression"); | ||||||
| } | ||||||
|
|
||||||
| // Check if the number of value provided matches the number of field. | ||||||
| let tuple_params = ty.get_rigid_ty_parameters().unwrap(); | ||||||
| let value_length = value_expressions.len(); | ||||||
| let declared_tuple_length = tuple_params.len(); | ||||||
|
|
||||||
| if value_length != declared_tuple_length { | ||||||
| bail!("The length of tuple declared is {declared_tuple_length}, the number of value provided is {value_length}"); | ||||||
| } | ||||||
|
|
||||||
| let mut value_tys: Vec<Ty> = Vec::new(); | ||||||
|
|
||||||
| for value_expression in value_expressions { | ||||||
| // FIXME: we only support const in value expression of tuple for now, we can add support | ||||||
| // more in future. | ||||||
| let Constant(_) = value_expression else { | ||||||
| bail!("Only Constant is supported in ValueExpression::Struct for now.") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| }; | ||||||
|
|
||||||
| value_tys.push(self.check_value( | ||||||
| typeck_env, | ||||||
| fn_assumptions, | ||||||
| value_expression, | ||||||
| )?); | ||||||
| } | ||||||
|
|
||||||
| // Make sure all the types supplied are the subtype of declared types. | ||||||
| self.prove_goal( | ||||||
| &typeck_env.env, | ||||||
| &fn_assumptions, | ||||||
| Wcs::all_sub(value_tys, tuple_params), | ||||||
| )?; | ||||||
|
|
||||||
| Ok(ty.clone()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, While tuple
(i32, u32)'s parameter is[i32, u32],struct
Foo's parameter is T.But I don't like how we need to go through
self.declsto get the struct field information.