-
Notifications
You must be signed in to change notification settings - Fork 157
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
Add check for path patterns. #3125
Add check for path patterns. #3125
Conversation
Let me resolve conflicts. |
c1fde94
to
9e1e0c7
Compare
* pattern. We should check that the declaration we are referencing IS NOT a | ||
* struct pattern or tuple with values. | ||
*/ | ||
// Pattern must be enum variants, sturcts, constants, or associated constansts |
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.
sturcts and constansts
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.
Thanks :)
/* | ||
* We are compiling a PathInExpression, which can't be a Struct or Tuple | ||
* pattern. We should check that the declaration we are referencing IS NOT a | ||
* struct pattern or tuple with values. | ||
*/ | ||
// Pattern must be enum variants, sturcts, constants, or associated constansts |
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.
Is this original comment incorrect? I thought PathInExpression could only reference a unit variant (not tuple variant or struct variant).
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.
Or would you get a Hir::PathInExpression for unit structs, aka
struct Unit;
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.
Oh the original message is okay but it lacks constants and associated constants
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 just split this comment to one here and one in line 100
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 guess my only problem is that instead of
" Pattern must be enum variants, sturcts, constants, or associated constansts"
It should be
" Pattern must be enum variants, unit structs, constants, or associated constants"
right?
Your new comment is more concise which is nice
maybe_item | ||
|= resolver->lookup_resolved_name (pattern.get_mappings ().get_nodeid (), | ||
&ref_node_id); | ||
maybe_item | ||
|= resolver->lookup_resolved_type (pattern.get_mappings ().get_nodeid (), | ||
&ref_node_id); |
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.
Why is there the same line twice? Am I missing something?
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.
First one checks variable names and second one checks type names. E.g. first for static items, second for type aliases
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'll leave this one to the judgement of the code maintainers. I'm not familiar with how this code works
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.
Maybe you dont recognize different methods are called
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.
oh lol. I'm sorry. I spent so long reading that code and didn't notice
rich_location rich_locus ( | ||
line_table, pattern.get_final_segment ().get_locus ()); | ||
rich_locus.add_fixit_replace ( | ||
"not a unit struct, unit variant or constatnt"); | ||
rust_error_at (rich_locus, ErrorCode::E0532, | ||
"expected unit struct, unit variant or constant, " | ||
"found %s %<%s%>", | ||
item_kind.c_str (), path_buf.c_str ()); | ||
return; |
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.
Much better error message :)
if (maybe_item) | ||
{ |
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.
If I understand correctly, lookup_resolved_type will fail if we need to do type inferencing. Is that 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.
AFAIK resolving (variable names and type names) is done before lowering AST into HIR.
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 section in rustc guide might be useful.
https://rustc-dev-guide.rust-lang.org/overview.html#lexing-and-parsing
gcc/testsuite/rust/compile/match8.rs
Outdated
|
||
static static_e: E = E::A(); | ||
|
||
struct S; |
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.
Shouldn't we also add a success test for unit structs?
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.
Yes, i also found cases for constants are missing:)
gcc/testsuite/rust/compile/match8.rs
Outdated
crate::type_alias => {} | ||
// { dg-error "expected unit struct, unit variant or constant, found type alias .crate::type_alias." "" { target *-*-* } .-1 } |
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.
Great test!
9e1e0c7
to
96ec7ca
Compare
I did not add tests for struct because HIR path prober for structs has not been implemented yet. Thus typecheck for struct path pattern (e.g. |
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.
LGTM, could you resolve conflicts ? I will merge upon resolution
gcc/rust/ChangeLog: * hir/tree/rust-hir.cc (Item::item_kind_string): New function. * hir/tree/rust-hir.h: New function. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Modify to check all arms in match expressions even if some of them has errors. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Add and fix check for path patterns. gcc/testsuite/ChangeLog: * rust/compile/issue-2324-2.rs: Fix error message. * rust/compile/match9.rs: New test. Signed-off-by: Raiki Tamura <[email protected]>
96ec7ca
to
a62e4d7
Compare
Thanks for review. Done :) |
Addresses #2553 #2094
Fix some incorrect error messages introduced in #3118