-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
base: main
Are you sure you want to change the base?
Conversation
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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));
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
#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.