Skip to content

[Clang] Fix dependence handling of nttp for variable templates #69075

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

Merged
merged 3 commits into from
Oct 17, 2023

Conversation

LYP951018
Copy link
Contributor

The dependence of a template argument is not only determined by the argument itself, but also by the type of the template parameter:

Furthermore, a non-type template-argument is dependent if the corresponding non-type template-parameter is of reference or pointer type and the template-argument designates or points to a member of the current instantiation or a member of a dependent type.

For example:

struct A{};

template <const A& T>
const A JoinStringViews = T;

template <int V>
class Builder {
public:
    static constexpr A Equal{};
    static constexpr auto Val = JoinStringViews<Equal>;
};

The constant expression Equal is not dependent, but because the type of the template parameter is a reference type and Equal is a member of the current instantiation, the template argument of JoinStringViews<Equal> is actually dependent, which makes JoinStringViews<Equal> dependent.

When a template-id of a variable template is dependent, CheckVarTemplateId will return an UnresolvedLookupExpr, but UnresolvedLookupExpr calculates dependence by template arguments only (the ConstantExpr Equal here), which is not dependent. This causes type deduction to think that JoinStringViews<Equal> is OverloadTy and treat it as a function template, which is clearly wrong.

This PR adds a KnownDependent parameter to the constructor of UnresolvedLookupExpr. After canonicalization, if CanonicalConverted contains any dependent argument, KnownDependent is set to true. This fixes the dependence calculation of UnresolvedLookupExpr for dependent variable templates.

Fixes #65153 .

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 14, 2023
@LYP951018 LYP951018 force-pushed the var_template_dependency_fix branch from 5f3557e to 7a5eef0 Compare October 14, 2023 18:58
@github-actions
Copy link

github-actions bot commented Oct 14, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@LYP951018 LYP951018 force-pushed the var_template_dependency_fix branch from 7a5eef0 to e7c7969 Compare October 14, 2023 19:20
@LYP951018 LYP951018 changed the title Fix dependence handling of nttp for variable templates [Clang] Fix dependence handling of nttp for variable templates Oct 14, 2023
@LYP951018
Copy link
Contributor Author

LYP951018 commented Oct 14, 2023

@erichkeane @cor3ntin could you review this PR, please? I’m not a collaborator, so I could not add reviewers by myself.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

This needs a release note, else I think it is OK.

@LYP951018 LYP951018 requested a review from erichkeane October 16, 2023 16:01
@LYP951018 LYP951018 force-pushed the var_template_dependency_fix branch from 443851f to 99f5d7e Compare October 16, 2023 16:20
@LYP951018 LYP951018 force-pushed the var_template_dependency_fix branch from 99f5d7e to 0ee8b54 Compare October 17, 2023 00:18
@LYP951018
Copy link
Contributor Author

@erichkeane could you help me merge this PR please? I could not merge it by myself because I don't have write access to the repo

@LYP951018 LYP951018 requested a review from MaskRay October 17, 2023 02:17
@LYP951018 LYP951018 force-pushed the var_template_dependency_fix branch from c357787 to dee8757 Compare October 17, 2023 03:21
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

A couple of nitpicks

TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
false, false),
TemplateKWLoc, NameInfo, TemplateArgs, Begin, End,
KnownDependent, false, false),
Copy link
Collaborator

Choose a reason for hiding this comment

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

/*KnownInstantiationDependent=*/false, /*KnownContainsUnexpandedParameterPack=*/false

to be consistent with bugprone-argument-comment

@@ -1299,8 +1299,9 @@ static bool checkTupleLikeDecomposition(Sema &S,
// in the associated namespaces.
Expr *Get = UnresolvedLookupExpr::Create(
S.Context, nullptr, NestedNameSpecifierLoc(), SourceLocation(),
DeclarationNameInfo(GetDN, Loc), /*RequiresADL*/true, &Args,
UnresolvedSetIterator(), UnresolvedSetIterator());
DeclarationNameInfo(GetDN, Loc), /*RequiresADL*/ true, &Args,
Copy link
Collaborator

Choose a reason for hiding this comment

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

/*RequiresADL=*/true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang crashes on variable template instantiated with a member of a template class
5 participants