Skip to content

Unfortunate error message for incorrect operator precedence between 'new' and '->' #10698

@chandlerc

Description

@chandlerc
Bugzilla Link 10326
Version unspecified
OS All

Extended Description

The following program is clearly invalid, but I think we could do better diagnosing it:

% cat x.cc
struct S {
S* foo(int) { return this; }
};

S* test() {
return new S()->foo(42);
}

% ./bin/clang -fsyntax-only x.cc
x.cc:6:17: error: expected ';' after return statement
return new S()->foo(42);
^
;
1 error generated.

It seems like it would be better to say in the error that the new operator has lower precedence than '->', and thus the new expression must be explicitly grouped. Then we could recover and continue parsing the rest of the expression as if that grouping had been performed. This seems reasonable to do for most of the postfix expressions which form a suffix but could not reasonable be part of a new-type-id: '->', '--', '++', and even '(...)' when the parentheses are not parsed as part of the new-initializer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions