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

Fix optional trait parsing #2745

Closed
wants to merge 1 commit into from

Conversation

dme2
Copy link
Contributor

@dme2 dme2 commented Nov 15, 2023

Addresses #2725

@powerboat9
Copy link
Contributor

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0363d352bef03bd91009d58c499ebf95

Comment on lines 4942 to 4947
if (lexer.peek_token()->get_id() == QUESTION_MARK) {
lexer.skip_token();
rust_error_at(lexer.peek_token()->get_locus(),
"%<?Trait%> is not permitted in supertraits");
return nullptr;
}
Copy link
Member

Choose a reason for hiding this comment

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

yes, as @powerboat9 showed we should not be emitting this error at the parsing stage, but probably in the typechecker. you can see that it parses properly in the playground link, or with a nightly rustc with the -Z parse-only CLI option

Copy link
Member

Choose a reason for hiding this comment

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

if you'd like any guidance for adding that check to the type checker please do say so :)

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 for the feedback. I'll go ahead and try to add the check to the typechecker.

@dme2 dme2 force-pushed the optional_trait_supertrait branch from c21374d to 2bce504 Compare November 25, 2023 22:42
@dme2
Copy link
Contributor Author

dme2 commented Dec 12, 2023

Hi @CohenArthur, I know the formatting isn't passing yet, but does that latest commit look ok?

Comment on lines 525 to 527
HIR::TraitBound &tb
= static_cast<HIR::TraitBound &> (*tp_bound.get ());
if (tb.get_polarity () == BoundPolarity::AntiBound)
Copy link
Member

Choose a reason for hiding this comment

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

this is good, but the static cast might fail if the trait bound is a lifetime.

you can switch on tp_bound.get_bound_type() and check if it is a lifetime or a trait, and proceed to cast safely then. so something like:

Suggested change
HIR::TraitBound &tb
= static_cast<HIR::TraitBound &> (*tp_bound.get ());
if (tb.get_polarity () == BoundPolarity::AntiBound)
if (tp_bound.get_bound_type() == LIFETIME) {
HIR::TraitBound &tb
= static_cast<HIR::TraitBound &> (*tp_bound.get ());
if (tb.get_polarity () == BoundPolarity::AntiBound)
/* snip */
}

etc. does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CohenArthur, thank you for the review. Do you have an example of the when the static cast might fail?

This program seems to compile fine with the code as is:

#[lang = "sized"]
pub trait Sized {}
trait Trait<'a>: 'a {}

Copy link
Member

Choose a reason for hiding this comment

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

ah, my bad - the static cast won't fail, but the value will be undefined. so we still need to do the check.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Check for ?Trait in visitor

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2725.rs: New test.

Signed-off-by: Dave Evans <[email protected]>
@dme2 dme2 force-pushed the optional_trait_supertrait branch from 2bce504 to 1429964 Compare December 31, 2023 22:54
@P-E-P
Copy link
Member

P-E-P commented Jun 12, 2024

@dme2 What is the current state of this PR ? From what I've read it seems like there is only a minor format issue ?

@P-E-P
Copy link
Member

P-E-P commented Jun 17, 2024

Closed in favor of #3055 (This is exactly the same PR with proper code format). Thank you for the initial work!

@P-E-P P-E-P closed this Jun 17, 2024
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