Bug 521469 - template specializations inside class scope aren't allowed by the standard
Summary: template specializations inside class scope aren't allowed by the standard
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-indexer (show other bugs)
Version: Next   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-28 08:36 EDT by Michael Woski CLA
Modified: 2020-09-04 15:18 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Woski CLA 2017-08-28 08:36:40 EDT
struct A {

  template<int I>
  struct Test{};

  template<>
  struct Test<0>{}; // <- not allowed
};

the template specialization should be marked as a syntactic problem.

If only I knew where/when to catch the erroneous declaration I would prepare a fix. Is it the parser itself or is there some AST visitor that is collecting such things?
Comment 1 Nathan Ridge CLA 2017-08-28 11:09:41 EDT
(In reply to Michael Woski from comment #0)
> If only I knew where/when to catch the erroneous declaration I would prepare
> a fix. Is it the parser itself or is there some AST visitor that is
> collecting such things?

CDT can produce both syntax errors, which are created by the parser, and semantic errors, most of which are created during binding resolution. When introducing a new error, we can choose which kind we want.

In this case, I would suggest producing a semantic error. (Semantic errors are generally preferable in cases where the AST is capable of representing the erroneous code.)

Semantic errors are usually created by having IASTName.resolveBinding() return an IProblemBinding for a particular name. The IDE will then surface this (via Codan's ProblemBindingChecker) by underlining the name in red. In this case, we'd want to do that for the template-id 'Test<0>' (note that ICPPASTTemplateId extends IASTName).

The creation of a binding for a template-id happens in CPPTemplates.createBinding(ICPPASTTemplateId), so you'll want to add a check there for the template-id being at class scope, and return an IProblemBinding in that case. You'll want to restrict your check to the case where isDefinition=true (because we only want to give an error for the definition of such an explicit specialization, not for references to it) and isExplicitSpecialization=true (since partial specializations are allowed at class scope).

Feel free to add a new error type to ISemanticProblem, and a corresponding error message (see [1] for an example of how to add the message). We have too many generic error messages for our current semantic problems, we could use some more specific ones.

[1] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=f9e4f1077a27abf3f2ea48d69aa9997ae51eaa93