This is an archive of the discontinued LLVM Phabricator instance.

[Concepts] Dump template arguments for immediately declared constraint.
ClosedPublic

Authored by hokein on Aug 5 2020, 1:26 AM.

Details

Summary

The template arguments were dumped as part of the TemplateTypeParmDecl, which
was incorrect.

Diff Detail

Event Timeline

hokein created this revision.Aug 5 2020, 1:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2020, 1:26 AM
hokein requested review of this revision.Aug 5 2020, 1:26 AM

This patch has helped me understand the difference between the two ConceptReferences in the AST (the TypeConstraint and the ConceptSpecializationExpr).

Given that

template <binary_concept<int> R>

is a shorthand for

template <typename R> requires binary_concept<R, int>

the TypeConstraint is the explicitly written binary_concept<int> whose getTemplateArgsAsWritten() has one element: <int>.

Meanwhile the ConceptSpecializationExpr is the implicit binary_concept<R, int> whose getTemplateArgsAsWritten() has two elements: <R, int>.

Is there a reason to prefer dumping one over the other (or even both)?

nridge added inline comments.Aug 5 2020, 11:56 PM
clang/test/AST/ast-dump-concepts.cpp
18

The ending column number here was important (it tested the fix to ConceptSpecializationExpr::getEndLoc() in D86413).

hokein updated this revision to Diff 283841.Aug 7 2020, 1:54 AM

address review comment.

hokein added a comment.Aug 7 2020, 1:57 AM

This patch has helped me understand the difference between the two ConceptReferences in the AST (the TypeConstraint and the ConceptSpecializationExpr).

Given that

template <binary_concept<int> R>

is a shorthand for

template <typename R> requires binary_concept<R, int>

the TypeConstraint is the explicitly written binary_concept<int> whose getTemplateArgsAsWritten() has one element: <int>.

Meanwhile the ConceptSpecializationExpr is the implicit binary_concept<R, int> whose getTemplateArgsAsWritten() has two elements: <R, int>.

Yes, true.

Is there a reason to prefer dumping one over the other (or even both)?

This is a good question. Currently we don't dump TypeConstrain instead we dump the responding immediately declared constraint (which is a ConceptSpecializationExpr).

I think it is ok to just dump the immediately declared constraint as TypeConstrain is just a wrapper of it.

template <binary_concept<int> R>

before this patch:
TemplateTypeParmDecl R
  |- ConceptSpecializationExpr
  `- TemplateArgument {{.*}} type 'int'

after this patch:
TemplateTypeParmDecl R
  |- ConceptSpecializationExpr
     |- TemplateArgument {{.*}} type 'R'
     `- TemplateArgument {{.*}} type 'int'

maybe the following is better:
TemplateTypeParmDecl R
  |- TypeContraint
     `- ConceptSpecializationExpr
         |- TemplateArgument {{.*}} type 'R'
         `- TemplateArgument {{.*}} type 'int'

The behavior (before this patch) is to dump the written template arguments in TypeConstraint as part of the TemplateTypeParmDecl, which doesn't seem to reasonable.
It also confuses the default template argument case.

template<typename T = int>

// is dump as 
TemplateTypeParmDecl  T
   `-TemplateArgument type 'int'
clang/test/AST/ast-dump-concepts.cpp
18

oh, I lost it during the rebase, added it back.

nridge accepted this revision.Aug 7 2020, 8:28 AM

Ok, sounds reasonable to me. Thanks!

This revision is now accepted and ready to land.Aug 7 2020, 8:28 AM