-
Notifications
You must be signed in to change notification settings - Fork 179
Add basic struct pattern rebinding #3741
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
Open
CohenArthur
wants to merge
3
commits into
Rust-GCC:master
Choose a base branch
from
CohenArthur:add-struct-pattern-rebinding
base: master
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ | |
// <http://www.gnu.org/licenses/>. | ||
|
||
#include "rust-compile-base.h" | ||
#include "rust-hir-pattern.h" | ||
#include "rust-hir-visitor.h" | ||
#include "rust-tyty.h" | ||
|
||
namespace Rust { | ||
namespace Compile { | ||
|
@@ -78,6 +80,19 @@ class CompilePatternBindings : public HIRCompileBase, | |
pattern.accept_vis (compiler); | ||
} | ||
|
||
tree make_struct_access (TyTy::ADTType *adt, TyTy::VariantDef *variant, | ||
Identifier &ident, int variant_index); | ||
|
||
void handle_struct_pattern_ident (HIR::StructPatternField &pat, | ||
TyTy::ADTType *adt, | ||
TyTy::VariantDef *variant, | ||
int variant_index); | ||
void handle_struct_pattern_ident_pat (HIR::StructPatternField &pat, | ||
TyTy::ADTType *adt, | ||
TyTy::VariantDef *variant, | ||
int variant_index); | ||
void handle_struct_pattern_tuple_pat (HIR::StructPatternField &pat); | ||
|
||
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. These should probably be 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. good catch thanks |
||
void visit (HIR::StructPattern &pattern) override; | ||
void visit (HIR::TupleStructPattern &pattern) override; | ||
void visit (HIR::ReferencePattern &pattern) override; | ||
|
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,19 @@ | ||
struct A { | ||
// the two warnings are invalid but this should be fixed by our lint rework | ||
// with this year's GSoC so ok for now | ||
a: i32, // { dg-warning "never read" } | ||
b: i32, // { dg-warning "never read" } | ||
} | ||
|
||
fn main() -> i32 { | ||
let a = A { a: 15, b: 14 }; | ||
|
||
let result = match a { | ||
A { | ||
a: self_a, | ||
b: self_b, | ||
} => self_a + self_b, | ||
}; | ||
|
||
result - 29 | ||
} |
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.
What's wrong with the old checks?
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.
they caused an invalid call to
struct_field_expression
which I didn't want to explore. since when compiling struct pattern matching withStructPatternFieldIdentPat
(somatch x { Foo { field: new_name } => {} }
we would always reach arust_unreachable()
, I think this code was actually never ever reached or executed and was just wrongThere 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.
Yeah, I think this was meant to handle struct-like enum items, but was probably broken by 806166d. Is there any chance you could fix it, rather than remove it? I could explain any parts of the code you have questions about, or fix it myself and give you the patch.
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.
but maybe I'm wrong? happy to change it and try to fix it instead if you think this was correct
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.
I think the code is close enough to right that getting it to work should be pretty simple -- check out where
match_scrutinee_expr
is being modified in the if statement above, you may have to remove that forstruct_field_expression
to work.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.
sounds good, I'll take a look then :) didn't realize it had been modified recently, my bad! I thought I was just hitting an uncharted part of the codebase lol
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.
Not all that recently -- I'm pretty sure I wrote it back in late 2023. You are probably right that it was never actually being executed, though I think it should function with a few tweaks