Skip to content

Conversation

@danakj
Copy link
Contributor

@danakj danakj commented Oct 17, 2025

Type check named constraint decls and definitions. We don't correctly error if you put a fn inside them. There is no support for require or alias yet, so there's nothing useful you can do with them yet.

We have attempted to share code between interface and constraint as they are quite similar. First by splitting out some of handle_interface.cpp to a separate file. Second by sharing some code paths when you want a facet type from either one, as they both turn into a facet type.

@danakj danakj requested a review from a team as a code owner October 17, 2025 21:30
@danakj danakj requested review from geoffromer and removed request for a team and geoffromer October 17, 2025 21:30
@danakj danakj requested a review from jonmeow October 17, 2025 21:31
@danakj
Copy link
Contributor Author

danakj commented Oct 17, 2025

Going to toss this toward @jonmeow for review. A lot of it is just boilerplate. I think the interesting question is what is the best way to share code between interface and constraint.

@jonmeow jonmeow requested a review from geoffromer October 17, 2025 23:37
@jonmeow
Copy link
Contributor

jonmeow commented Oct 17, 2025

I did a quick pass, but I'm going to be off next week so re-adding @geoffromer who got the initial autoassign.

@danakj danakj requested review from geoffromer and removed request for geoffromer October 20, 2025 17:26
@danakj
Copy link
Contributor Author

danakj commented Oct 20, 2025

I did a quick pass, but I'm going to be off next week so re-adding @geoffromer who got the initial autoassign.

Oops, thanks, I removed him as I thought something went wrong. Re-added back. But @geoffromer you can hold off reviewing until I address this feedback. I will ping you when it's ready.

Copy link
Contributor

@geoffromer geoffromer left a comment

Choose a reason for hiding this comment

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

But @geoffromer you can hold off reviewing until I address this feedback. I will ping you when it's ready.

Sounds good. (Replying here so the bot stops pinging me :-) )

Copy link
Contributor Author

@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.

PTAL

@danakj danakj requested a review from geoffromer October 20, 2025 21:10
@danakj danakj requested a review from geoffromer October 22, 2025 15:50
Copy link
Contributor Author

@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.

PTAL

Comment on lines +245 to +265
template <typename EntityT>
requires std::same_as<EntityT, SemIR::Interface>
static auto TryGetEntity(Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::InterfaceDecl>()) {
return &context.interfaces().Get(decl->interface_id);
} else {
return nullptr;
}
}

template <typename EntityT>
requires std::same_as<EntityT, SemIR::NamedConstraint>
static auto TryGetEntity(Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::NamedConstraintDecl>()) {
return &context.named_constraints().Get(decl->named_constraint_id);
} else {
return nullptr;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: I think you can make these into a conventional overload set with something like this:

Suggested change
template <typename EntityT>
requires std::same_as<EntityT, SemIR::Interface>
static auto TryGetEntity(Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::InterfaceDecl>()) {
return &context.interfaces().Get(decl->interface_id);
} else {
return nullptr;
}
}
template <typename EntityT>
requires std::same_as<EntityT, SemIR::NamedConstraint>
static auto TryGetEntity(Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::NamedConstraintDecl>()) {
return &context.named_constraints().Get(decl->named_constraint_id);
} else {
return nullptr;
}
}
template <typename T>
struct Tag {};
static auto TryGetEntity(Tag<SemIR::Interface>, Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::InterfaceDecl>()) {
return &context.interfaces().Get(decl->interface_id);
} else {
return nullptr;
}
}
static auto TryGetEntity(Tag<SemIR::NamedConstraint>, Context& context, SemIR::Inst inst)
-> const SemIR::EntityWithParamsBase* {
if (auto decl = inst.TryAs<SemIR::NamedConstraintDecl>()) {
return &context.named_constraints().Get(decl->named_constraint_id);
} else {
return nullptr;
}
}

Then the callsite looks like TryGetEntity(Tag<EntityT>{}, context, existing_decl_inst).

This technique trades off a slightly more awkward interface for a more straightforward implementation, so I'd be more hesitant to do it with a public function, but it can be a good fit for implementation details within a .cpp file.

@danakj danakj requested a review from geoffromer October 22, 2025 18:18
@danakj
Copy link
Contributor Author

danakj commented Oct 22, 2025

My reply here isn't showing easily on the convo tab so surfacing it for you.

@geoffromer geoffromer added this pull request to the merge queue Oct 22, 2025
Merged via the queue into carbon-language:trunk with commit 22580a4 Oct 22, 2025
8 checks passed
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.

3 participants