Skip to content

[Clang][RFC] Resugar attributed type alias #143143

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Jun 6, 2025

#142608 reflects a case where the type sugar on the attributed type alias is lost.

It might be possible to add the sugar back at the cost of duplicating the type alias decl, along with a modified type source info.

Though I'm not sure if the solution is feasible, and since there's no test regression I'll just elaborate the difference through a diagnostic.

Comment on lines +7016 to +7033
auto *ET = cast<TypedefType>(Old);
QualType Underlying = wrap(C, ET->desugar(), I);
TypedefNameDecl *Typedef;
if (auto *TD = dyn_cast<TypedefDecl>(ET->getDecl())) {
Typedef = TypedefDecl::Create(C, TD->getDeclContext(),
TD->getBeginLoc(), TD->getLocation(),
TD->getIdentifier(), nullptr);
Typedef->setModedTypeSourceInfo(TD->getTypeSourceInfo(), Underlying);
} else {
auto *Alias = cast<TypeAliasDecl>(ET->getDecl());
Typedef = TypedefDecl::Create(
C, Alias->getDeclContext(), Alias->getBeginLoc(),
Alias->getLocation(), Alias->getIdentifier(), nullptr);
Typedef->setModedTypeSourceInfo(Alias->getTypeSourceInfo(),
Underlying);
}
Typedef->setPreviousDecl(ET->getDecl());
return C.getTypedefType(Typedef, Underlying);
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
auto *ET = cast<TypedefType>(Old);
QualType Underlying = wrap(C, ET->desugar(), I);
TypedefNameDecl *Typedef;
if (auto *TD = dyn_cast<TypedefDecl>(ET->getDecl())) {
Typedef = TypedefDecl::Create(C, TD->getDeclContext(),
TD->getBeginLoc(), TD->getLocation(),
TD->getIdentifier(), nullptr);
Typedef->setModedTypeSourceInfo(TD->getTypeSourceInfo(), Underlying);
} else {
auto *Alias = cast<TypeAliasDecl>(ET->getDecl());
Typedef = TypedefDecl::Create(
C, Alias->getDeclContext(), Alias->getBeginLoc(),
Alias->getLocation(), Alias->getIdentifier(), nullptr);
Typedef->setModedTypeSourceInfo(Alias->getTypeSourceInfo(),
Underlying);
}
Typedef->setPreviousDecl(ET->getDecl());
return C.getTypedefType(Typedef, Underlying);
return C.getTypedefType(cast<TypedefType>(Old)->getDecl(), Underlying);

Copy link
Contributor

Choose a reason for hiding this comment

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

Otherwise, passing that second 'Underlying' argument would be unnecessary.

The reason we have that second parameter is to support a feature called 'divergent typedefs', which was added a couple of years ago in order to make resugaring more efficient.

This is currently used by the getCommonSugaredType infrastructure as well.

But yeah, this allows you to invent a different underlying type for a TypedefType, without having to create a fake TypedefDecl for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But yeah, this allows you to invent a different underlying type for a TypedefType, without having to create a fake TypedefDecl for it.

No? There's an assert in that function:

assert(hasSameType(Decl->getUnderlyingType(), Underlying));

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's has same type, not identical types. Are we not just modifying the type sugar 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.

The context of this resugar is to find the FunctionType and add an attribute (by creating a new function type) on it.

The resugarer tries to add back anything it gets rid of, like the TypedefType in this case.

Or do you know if there's other way around to avoid copying the function type?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, but the new function type is still the same as the original one, it's just not identical (different sugar).

If that's the case, that's still under contract with getTypedefType.

If that's not the case, I think it would be possible to remove that assert, you just need to investigate and deal with the consequences elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll take a look

Copy link
Contributor

Choose a reason for hiding this comment

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

In both cases, either divergent typedef or creating a new decl, if the types are not the same, you have to deal with these consequences.

For example, getCommonSugaredType expects all TypedefTypes of the same typedef declaration to be the same type.

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