This is an archive of the discontinued LLVM Phabricator instance.

[AST] dont invaliate VarDecl when the initializer contains errors.
ClosedPublic

Authored by hokein on Apr 14 2020, 8:35 AM.

Details

Summary

This might be splitted into 2 separate patchs:

  1. the initializer of a variable should play no part in decl "invalid" bit;
  2. preserve the exprs in an error initializer via recovery exprs;

if we do 1) only, we will regress the diagnostics (one big regression is that
we loose the "selected 'begin' function with iterator type" diagnostic in for-range stmt;
with 2) together, we don't have regressions (the new diagnostics seems to be
improved).

Diff Detail

Event Timeline

hokein created this revision.Apr 14 2020, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2020, 8:35 AM
sammccall added inline comments.Apr 14 2020, 12:31 PM
clang/lib/Sema/SemaDecl.cpp
11998

if the variable is an undeduced auto (and the type of the recoveryexpr is unknown), we still have to mark it invalid. Does that happen somewhere else?

clang/test/AST/ast-dump-invalid-initialized.cpp
21

what actually is the AST here? invalid() is a recoveryexpr, does it get wrapped in a second recoveryexpr by the code in this patch? Or is this testing existing behavior?

clang/test/CXX/stmt.stmt/stmt.iter/stmt.ranged/p1.cpp
3

not sure we should be switching this until we flip the default.
Are we regressing the diagnostics under default flags?

(and in 2 other files)

hokein updated this revision to Diff 258169.Apr 16 2020, 2:10 PM
hokein marked 5 inline comments as done.

address comments

hokein added inline comments.Apr 16 2020, 2:12 PM
clang/lib/Sema/SemaDecl.cpp
11998

Yeah, the auto case is handled earlier (Line 11845 of this file), so we are safe at this point.

clang/test/AST/ast-dump-invalid-initialized.cpp
21

the AST looks like:

-VarDecl 0x9b80970 <col:3, col:17> col:5 a6 'A' callinit
       `-ParenListExpr 0x9b80ac0 <col:7, col:17> 'NULL TYPE' contains-errors
         `-RecoveryExpr 0x9b80a78 <col:8, col:16> '<dependent type>' contains-errors lvalue
           `-UnresolvedLookupExpr 0x9b809d8 <col:8> '<overloaded function type>' lvalue (ADL) = 'invalid' empty

my attemption was to verify the valid-bit of VarDecl, but they can be covered by ast-dump-recovery.cpp.

clang/test/CXX/stmt.stmt/stmt.iter/stmt.ranged/p1.cpp
3

yes, we have a few regressions without -frecovery-ast, the worst regression is that we stop emitting the "selected 'begin' function with iterator type 'Data *'" diagnostic note in an ill-formed range-range stmt.

As discussed in another patch, lets not change the default flag in existing tests, reverted the new flag, adjusted exiting tests, and created a new test for -frecovery-ast.

Most of the new diagnostics look good, as you say.

I'm okay with the regression around foreach loops, given that:

  • per rsmith, the mechanism behind this diagnostic was misdesigned (relying on initializer making things invalid)
  • it's a C++-only features and we should be able to flip on recoveryexpr for C++ soon
clang/test/SemaCXX/constant-expression-cxx11.cpp
880–882

I'm not *sure* what the purpose of these test is, but my guess is they're mostly trying to test nullpointer comparisons not error recovery. maybe we should have 1 with nullB and the error, and the rest use nullB1?

sammccall accepted this revision.Apr 16 2020, 2:45 PM

oops, meant to approve

This revision is now accepted and ready to land.Apr 16 2020, 2:45 PM
This revision was automatically updated to reflect the committed changes.
hokein marked an inline comment as done.