Skip to content

Conversation

@hamishknight
Copy link
Contributor

Make sure the destination type actually conforms to the corresponding builtin literal protocol before attempting to import it. We may want to consider allowing any type that conforms to the non-builtin literal protocol, but I want to keep this patch low risk and just ensure we at least don't crash for now.

rdar://156524292

@hamishknight hamishknight force-pushed the fishers-landing branch 4 times, most recently from 1c8f747 to 17f5c11 Compare November 10, 2025 12:41
if (!ctx.getIntBuiltinInitDecl(constantTyNominal))
return nullptr;

return createMacroConstant(Impl, MI, name, DC, constantType,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to pass down the result of the lookup to createMacroConstant() instead of re-doing the lookup inside?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cache the result on success, so it shouldn't matter much. There are also a bunch of different clients of createConstant, which would make it a bit awkward (although looking at them it's not entirely clear to me that we have the correct checks for them either)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly the caching logic in getBuiltinInitDecl only uses the decl as the key, I guess we never ever conform a decl to more than 1 builtin literal kind? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm nope, Float conforms to both _ExpressibleByBuiltinIntegerLiteral & _ExpressibleByBuiltinFloatLiteral

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to also include the kind in the key


auto &ctx = DC->getASTContext();
auto *constantTyNominal = constantType->getAnyNominal();
if (!constantTyNominal)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add an assert at the other call sites too then, to avoid propagating nulls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm not sure I understand what you mean by this

Allow querying for types that may not conform. Failures here should
be asserted downstream (e.g in `setBuiltinInitializer`).
Make sure we don't return the wrong witness if you query with 2
different builtin protocol kinds.
Make sure the destination type actually conforms to the corresponding
builtin literal protocol before attempting to import it. We may want
to consider allowing any type that conforms to the non-builtin literal
protocol, but I want to keep this patch low risk and just ensure we at
least don't crash for now.

rdar://156524292
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test Windows

@hamishknight hamishknight merged commit 42cfda1 into swiftlang:main Nov 11, 2025
5 checks passed
@hamishknight hamishknight deleted the fishers-landing branch November 11, 2025 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants