Skip to content
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

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

tamaroning
Copy link
Contributor

@tamaroning tamaroning commented Aug 10, 2024

Addresses #2553 #2094
Fix some incorrect error messages introduced in #3118

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/match8.rs: New test.

@tamaroning
Copy link
Contributor Author

Let me resolve conflicts.

* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sturcts and constansts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

Comment on lines -48 to +46
/*
* 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
Copy link
Contributor

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).

Copy link
Contributor

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;

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor

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

Comment on lines +51 to +56
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);
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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

Comment on lines +87 to +98
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better error message :)

Comment on lines +58 to +60
if (maybe_item)
{
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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


static static_e: E = E::A();

struct S;
Copy link
Contributor

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?

Copy link
Contributor Author

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:)

Comment on lines 17 to 19
crate::type_alias => {}
// { dg-error "expected unit struct, unit variant or constant, found type alias .crate::type_alias." "" { target *-*-* } .-1 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test!

@tamaroning
Copy link
Contributor Author

tamaroning commented Aug 12, 2024

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. crate::S => {}) seems to fail.
tests for them will be added once it is implemented.

https://github.com/tamaroning/gccrs/blob/96ec7ca1e096e8ba42632b76410d5cbf978b94cb/gcc/rust/typecheck/rust-hir-path-probe.h#L29-L44

Copy link
Member

@P-E-P P-E-P left a 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]>
@tamaroning
Copy link
Contributor Author

Thanks for review. Done :)

@CohenArthur CohenArthur added this pull request to the merge queue Aug 28, 2024
Merged via the queue into Rust-GCC:master with commit 5da381f Aug 28, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants