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

Avoid poisoning non identifier names #4884

Merged
merged 5 commits into from
Feb 13, 2025

Conversation

bricknerb
Copy link
Contributor

@bricknerb bricknerb commented Feb 3, 2025

There are different use cases where we call Context::LookupQualifiedName() on non identifiers, like NameId::SelfType, which implicitly triggers poisoning these names. I don't think poisoning non identifiers likeSelf is ever useful.

Use cases where Self is being poisoned:

Part of #4622.

There are different use cases where we call `Context::LookupQualifiedName()` on `NameId::SelfType`, which implicitly triggers poisoning this name.
I don't think poisoning `Self` is ever useful, and it's likely that's true for other (if not all) special names (`Self` seems to be the most common use case though).

Use cases where `Self` is being poisoned:
* Checking allowed access: https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/member_access.cpp#L62, for example, in https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/testdata/alias/no_prelude/fail_aliased_name_in_diag.carbon#L21
* Using the type `Self` as a parameter type: https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/core/prelude/operators/arithmetic.carbon#L78

Part of carbon-language#4622.
@bricknerb bricknerb requested a review from jonmeow February 3, 2025 16:02
@github-actions github-actions bot requested a review from zygoloid February 3, 2025 16:02
@bricknerb bricknerb enabled auto-merge February 6, 2025 15:47
Comment on lines 429 to 431
if (auto entry_id = is_being_declared || name_id == SemIR::NameId::SelfType
? scope.Lookup(name_id)
: scope.LookupOrPoison(name_id)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you point to a test where this fires? At trunk, I added here:

  CARBON_CHECK(!is_being_declared || (name_id == SemIR::NameId::Base ||
                                      name_id.AsIdentifierId().has_value()),
               "{0}", name_id);

And that seems to pass. But also, that could just be put into LookupOrPoison without changing this code if you wanted to just assert that the code was never used that way. What am I missing that you're special-casing SelfType here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How does this assert help checking this?

This is what I added, which fails:

  CARBON_CHECK(is_being_declared || name_id != SemIR::NameId::SelfType, "{0}",
               name_id);

I've encountered that when I tried to add poisoned names to the formatter and noticed that in a lot of cases, Self is being poisoned.
I don't think poisoning Self has an effect except costing more resources.

I wanted to clean this before trying to format poisoning names (which might be debateable regardless) so there we won't see a lot of file diffs because of that unnecessary poisoning.

Copy link
Contributor

@jonmeow jonmeow Feb 11, 2025

Choose a reason for hiding this comment

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

Sorry, maybe I'm being unclear here.

You're adding "|| name_id == SemIR::NameId::SelfType". I'm saying, I tried adding a CHECK that that condition was never true, and it seems to never be true. What am I missing? Why does it need to be handled?

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've changed the logic of LookupOrPoison() to not poison non identifiers and improved the unit tests to properly demonstrate that.

Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

Sorry, I forgot to comment on this PR before, but maybe you can add an integration test that demonstrates the change in behavior?

@bricknerb
Copy link
Contributor Author

Sorry, I forgot to comment on this PR before, but maybe you can add an integration test that demonstrates the change in behavior?

There are a lot of tests that trigger that, but you don't see the impact because poisoning Self doesn't impact the output, just costs more resources AFAICT.

@bricknerb bricknerb requested a review from jonmeow February 11, 2025 22:40
//
// This cannot be used to lookup `SelfType`; use `Lookup` instead.
// poisons the name so it can't be declared later. Names that are not
// identifiers would not be poisoned.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// identifiers would not be poisoned.
// identifiers will not be poisoned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@bricknerb bricknerb changed the title Avoid poisoning Self Avoid poisoning non identifier names Feb 13, 2025
@bricknerb bricknerb added this pull request to the merge queue Feb 13, 2025
Merged via the queue into carbon-language:trunk with commit 23e5677 Feb 13, 2025
8 checks passed
@bricknerb bricknerb deleted the poison6 branch February 13, 2025 21:16
bricknerb added a commit to bricknerb/carbon-lang that referenced this pull request Feb 13, 2025
github-merge-queue bot pushed a commit that referenced this pull request Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants