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

Use FacetAccessType when converting to a value of type FacetType #4925

Merged
merged 3 commits into from
Feb 12, 2025

Conversation

danakj
Copy link
Contributor

@danakj danakj commented Feb 11, 2025

When attempting to convert to a value of type FacetType, and the source value is a FacetAccessType, then if the type of the underlying FacetValue is the same as the target, we can use the FacetValue there as the conversion output.

If the type of the FacetValue differs, then we still want to do impl lookup with the FacetValue to see if it matches with the target FacetType, but that is still a TODO.

This allows a generic function with a value whose type is constrained by a FacetType (thus the value's type is a FacetAccessType), to call other functions with the value as an argument when it is constrained by the same FacetType:

fn F[T:! FacetType](x: T);
fn G[T:! FacetType](x: T) { F(x); }

It is also an optimization to avoid impl lookup where we've already done it to produce the FacetAccessType.

Adds a bunch of new tests with values of types which are constrained by a facet type (or "facet value value" for short), with some more tests that currently fail and should be made to pass.

@danakj danakj force-pushed the reuse-facetaccesstype branch from 2dff78a to 5c758da Compare February 11, 2025 21:12
@github-actions github-actions bot requested a review from zygoloid February 11, 2025 21:12
When attempting to convert to a value of type FacetType, and the source
value is a FacetAccessType, then if the type of the underlying
FacetValue is the same as the target, we can use the FacetValue there
as the conversion output.

If the type of the FacetValue differs, then we still want to do impl
lookup with the FacetValue to see if it matches with the target
FacetType, but that is still a TODO.

This allows a generic function with a value whose type is constrained
by a FacetType (thus the value's type is a FacetAccessType), to call
other functions with the value as an argument when it is constrained
by the same FacetType:
```
fn F[T:! FacetType](x: T);
fn G[T:! FacetType](x: T) { F(x); }
```

It is also an optimization to avoid impl lookup where we've already done
it to produce the FacetAccessType.

Adds a bunch of new tests with values of types which are constrained by
a facet type (or "facet value value" for short), with some more tests
that currently fail and should be made to pass.
@danakj danakj force-pushed the reuse-facetaccesstype branch from 5c758da to 34220ff Compare February 11, 2025 21:59
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.

Looks great, thanks.

@@ -991,7 +991,24 @@ static auto PerformBuiltinConversion(Context& context, SemIR::LocId loc_id,
}

if (sem_ir.types().Is<SemIR::FacetType>(target.type_id)) {
if (sem_ir.types().Is<SemIR::FacetType>(value_type_id)) {
auto lookup_inst = value_id;
Copy link
Contributor

Choose a reason for hiding this comment

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

We'd usually call this lookup_inst_id instead to indicate that it's an InstId not an Inst.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, done. Thanks for the review!

@danakj danakj enabled auto-merge February 12, 2025 14:14
@danakj danakj added this pull request to the merge queue Feb 12, 2025
Merged via the queue into carbon-language:trunk with commit 30b2b5e Feb 12, 2025
8 checks passed
@danakj danakj deleted the reuse-facetaccesstype branch February 12, 2025 14:53
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