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

When looking up Core, pass is_being_declared as true #4903

Closed
wants to merge 1 commit into from

Conversation

bricknerb
Copy link
Contributor

@bricknerb bricknerb commented Feb 6, 2025

There's no need to try and poison Core.
This doesn't change tests since Core isn't being poisoned anyways.
Part of #4622.

@@ -369,6 +369,16 @@ fn F() {
}
}

// --- fail_dont_implicitly_poison_core.carbon

// CHECK:STDERR: fail_dont_implicitly_poison_core.carbon:[[@LINE+4]]:9: error: `Core.Bool` implicitly referenced here, but package `Core` not found [CoreNotFound]
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this error to be pretty confusing. I would easily try to fix it by trying to understand why Core is not found - is my installation messed up? Where is Core? But of course the answer is I named something Core later in the file.

That said, I think the way we want to resolve this is via https://github.com/carbon-language/carbon-lang/pull/4864/files, which is to make Core a keyword. So the class Core would not in fact make a class called Core, it would be an invalid use of the Core keyword. Then we should not even have an error here, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIU, actually, the reason Core is not found is because this is a no_prelude test so Core is not loaded.
It's not found similar to other no_prelude tests.
This test is different from other tests that trigger this error because it shows that this doesn't cause Core to be poisoned, unlike #4900.

If Core is a keyword, we would still have this error.
We might have another error saying that class Core cannot be defined because Core is a keyword.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh okay, thanks. I misunderstood the relationship to #4900.

The failure looks good then, but worrying about poisoning Core specifically is a transient concern, since we won't be able to write class Core once we make it a keyword. At that point would we just remove this test? Do you think it's worth adding still as-is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to class r#Core.

@@ -703,7 +703,7 @@ static auto GetCorePackage(Context& context, SemIR::LocId loc_id,
// Look up `package.Core`.
auto core_scope_result = context.LookupNameInExactScope(
loc_id, core_name_id, SemIR::NameScopeId::Package,
context.name_scopes().Get(SemIR::NameScopeId::Package));
context.name_scopes().Get(SemIR::NameScopeId::Package), true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I do think this is the right change to make - not sure how the keyword impl shakes out but we don't want to poison things.

Suggested change
context.name_scopes().Get(SemIR::NameScopeId::Package), true);
context.name_scopes().Get(SemIR::NameScopeId::Package), /*is_being_declared=*/true);

But I am not sure how to test this in a way that would survive the keyword change, and welcome thoughts on that.

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 (and changed to r#Core).

// CHECK:STDERR:
fn F(x: bool);

class Core {}
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
class Core {}
class r#Core {}

Would this test the same thing now - and after the keyword change?

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 believe so, done.

@jonmeow
Copy link
Contributor

jonmeow commented Feb 6, 2025

There's an open proposal to make Core a keyword. I think #4900 is more correct.

Copy link
Contributor

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

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

Perhaps we should stop using name lookup to find the Core package. We could instead track the Core package specially on the SemIR::File.

@bricknerb
Copy link
Contributor Author

Perhaps we should stop using name lookup to find the Core package. We could instead track the Core package specially on the SemIR::File.

This makes sense though I assume there might be complexity cost for special casing Core.

@bricknerb bricknerb requested review from zygoloid and danakj February 7, 2025 09:30
@danakj
Copy link
Contributor

danakj commented Feb 7, 2025

There's an open proposal to make Core a keyword. I think #4900 is more correct.

Yeah, I can't tell which of the two PRs is more correct myself, but maybe we should rebase either one on #4909 at which point r#Core won't be poisoned, so the test in here should pass without any code changes?

github-merge-queue bot pushed a commit that referenced this pull request Feb 10, 2025
Alternatively, if poisoning `Core` is considered a bug, we could avoid
poisoning it (see #4903).

Part of #4622.
Copy link
Contributor

@danakj danakj left a comment

Choose a reason for hiding this comment

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

LGTM

@bricknerb
Copy link
Contributor Author

I think other changes made this change different, so I'm moving this to draft.

@bricknerb bricknerb marked this pull request as draft February 13, 2025 23:02
…ere's no need to try and poison `Core`.

This doesn't change tests since `Core` isn't being poisoned anyways.
Part of carbon-language#4622.
@bricknerb bricknerb changed the title Avoid implicitly poisoning Core When looking up Core, pass is_being_declared as true Feb 14, 2025
@bricknerb bricknerb marked this pull request as ready for review February 14, 2025 00:22
@github-actions github-actions bot requested a review from chandlerc February 14, 2025 00:23
@bricknerb
Copy link
Contributor Author

Sorry for the force push, I had to redo this change given recent changes.

@bricknerb bricknerb requested a review from danakj February 14, 2025 00:23
@jonmeow
Copy link
Contributor

jonmeow commented Feb 14, 2025

@bricknerb Given #4884, isn't this change a no-op? I ask because also, isn't setting "is_being_declared" when we're doing a non-declaring name lookup a little confusing for future readers?

@bricknerb
Copy link
Contributor Author

bricknerb commented Feb 14, 2025

@bricknerb Given #4884, isn't this change a no-op?

Yes!

I ask because also, isn't setting "is_being_declared" when we're doing a non-declaring name lookup a little confusing for future readers?

The name is_being_declared might be misleading. It's currently set to true only when called from LookupNameInDecl() in a scope. In other words, when the name being looked up is known to be in a specific scope.
That's the case here, we know the scope of Core, so it makes more sense to pass true.
WDYT?

@jonmeow
Copy link
Contributor

jonmeow commented Feb 14, 2025

@bricknerb Given #4884, isn't this change a no-op?

Yes!

I ask because also, isn't setting "is_being_declared" when we're doing a non-declaring name lookup a little confusing for future readers?

The name is_being_declared might be misleading. It's currently set to true only when called from LookupNameInDecl() in a scope. In other words, when the name being looked up is known to be in a specific scope. That's the case here, we know the scope of Core, so it makes more sense to pass true. WDYT?

I think that Core is a top-level name, and should pretty much always be there (even before #4884, poisoning would have rarely run and could have poisoned at most one namespace). This is not declaring it, so it will only confuse readers to set "is_being_declared". This PR should be closed, not merged (to be clear, this is what I also meant when I said #4900 is more correct).

@jonmeow
Copy link
Contributor

jonmeow commented Feb 14, 2025

BTW, LookupNameInDecl sets is_being_declared to true because it's part of declaring names. I think the name is correct.

@danakj
Copy link
Contributor

danakj commented Feb 14, 2025

@bricknerb Given #4884, isn't this change a no-op?

Yes!

I ask because also, isn't setting "is_being_declared" when we're doing a non-declaring name lookup a little confusing for future readers?

The name is_being_declared might be misleading. It's currently set to true only when called from LookupNameInDecl() in a scope. In other words, when the name being looked up is known to be in a specific scope. That's the case here, we know the scope of Core, so it makes more sense to pass true. WDYT?

I think that Core is a top-level name, and should pretty much always be there (even before #4884, poisoning would have rarely run and could have poisoned at most one namespace). This is not declaring it, so it will only confuse readers to set "is_being_declared". This PR should be closed, not merged (to be clear, this is what I also meant when I said #4900 is more correct).

Hm yeah, when I reviewed earlier it was just a test showing that using Core implicitly did not prevent r#Core from being used. Verifying r#Core works is good. But I agree we don't need to pass is_being_declared for Core - it's not declaring it, it's a keyword.

@bricknerb
Copy link
Contributor Author

bricknerb commented Feb 14, 2025

BTW, LookupNameInDecl sets is_being_declared to true because it's part of declaring names. I think the name is correct.

Sure, in LookupNameInDecl() context it's correct, just not sure that it would stay correct in LookupNameInExactScope() context for any (future?) call, given how it's used.
I guess declaring and poisoning together does make sense in principal.

@bricknerb
Copy link
Contributor Author

@bricknerb Given #4884, isn't this change a no-op?

Yes!

I ask because also, isn't setting "is_being_declared" when we're doing a non-declaring name lookup a little confusing for future readers?

The name is_being_declared might be misleading. It's currently set to true only when called from LookupNameInDecl() in a scope. In other words, when the name being looked up is known to be in a specific scope. That's the case here, we know the scope of Core, so it makes more sense to pass true. WDYT?

I think that Core is a top-level name, and should pretty much always be there (even before #4884, poisoning would have rarely run and could have poisoned at most one namespace). This is not declaring it, so it will only confuse readers to set "is_being_declared". This PR should be closed, not merged (to be clear, this is what I also meant when I said #4900 is more correct).

SG, closing.

@bricknerb bricknerb closed this 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.

4 participants