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 Issue 3351: Shared discriminator having the same value< #3458

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

badumbatish
Copy link
Contributor

@badumbatish badumbatish commented Feb 26, 2025

Fixes #3351

Overriding on the compile item visitor, I am not sure if this is the right way to go?

@badumbatish
Copy link
Contributor Author

redundant header include, i have noticed, will fix after review

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

Fantastic work i've left you comments for style. The error message using %< etc will fix the warnings that are introduced.

I don't think you should need the changes to rust-compile-item.cc but left me know if removing it causes an issue for you

I think in theory we might need a guard in your code where check_variant_record_collision needs to check if this has been checked before because you will get duplicate warnings here for example:

#[lang="sized"]
trait Sized {}

enum Greeting<T> {
    Hi(T),
    Bye
}

fn test() {
    let a = Greeting::<i32>::Hi(123);
    let b = Greeting::<f32>::Bye;
}

So I think lets get this first part merged. Then you will need to start tracking the HIR::Enum def-id on the TyTy::ADTType and you can keep a map in the backend if this DefID has already been checked to solve the above problem.

@philberty philberty added the diagnostic diagnostic static analysis label Feb 26, 2025
@philberty philberty added the bug label Feb 26, 2025
@badumbatish badumbatish force-pushed the issue_3351 branch 3 times, most recently from c59acb0 to 08229bd Compare February 27, 2025 08:30
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

LGTM nice work

@badumbatish
Copy link
Contributor Author

LGTM nice work

thank you for your guidance!


rich_location r (line_table, type.get_locus ());
std::string assigned_here_msg
= "`" + std::to_string (discriminant_integer) + "`" + " assigned here";
Copy link
Member

Choose a reason for hiding this comment

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

There are more backquote here, GCC probably does't complain about those because they are not in the format string. I'm unsure if we should change them.

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 can change it to %qlld via format string i think? The reason i did it this way is because different platform has different width for HOST WIDTH INT and im not sure if the final answer would be %qlld or %qld or if there is any complication if HWI is only %d and we use %qlld

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm kinda torn on the subject. Raw back quotes don't play well with different locales, this is not only a matter of style.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would HOST_WIDE_INT_PRINT_DEC work?

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 think it could work for rust_error_at,

for add_fixit_replace() they dont take formatting arguments, is there a way to create a standalone formatting string in the codebase without explicitly using snprintf?

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 use normal quotation marks (") instead of ` and include a TODO comment, so we're more likely to notice and fix this in the future

i will opt for this. fix coming soon

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, wait, we have rust_open_quote and rust_close_quote -- that should do the trick

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, wait, check out expand_message in rust-diagnostics.cc -- that should be what you're looking for

Copy link
Contributor Author

@badumbatish badumbatish Mar 7, 2025

Choose a reason for hiding this comment

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

Oh, wait, check out expand_message in rust-diagnostics.cc -- that should be what you're looking for

the function is only in rust-diagnostics.cc, im not sure if i should move it to the header to use it solely for rust-compile-type.cc?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could make it not-static and add a declaration to rust-diagnostics.h

gcc/testsuite/ChangeLog:

	* rust/compile/shared_discriminator_enum.rs: New test.
gcc/rust/ChangeLog:

	* backend/rust-compile-item.cc (CompileItem::visit):
	Use HIR::Enum visit
	* backend/rust-compile-item.h: Fix clash of enum shared
	discriminant
	* backend/rust-compile-type.cc (check_variant_record_collision):
	Likewise.
	(TyTyResolveCompile::visit): Early return if fail enum check
	* rust-diagnostics.cc (expand_message): non-static now.
	* rust-diagnostics.h (RUST_ATTRIBUTE_GCC_DIAG): non-static
	expand msg
	(expand_message): likewise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug diagnostic diagnostic static analysis
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Shared discriminator does not cause compiler error
4 participants