This is an archive of the discontinued LLVM Phabricator instance.

[clang] Improve diagnostics for auto-return-type function if the return expr had an error
ClosedPublic

Authored by hokein on Nov 27 2020, 12:32 AM.

Details

Summary

Given the following case:

auto k() {
  return undef();
  return 1;
}

Prior to the patch, clang emits an `cannot initialize return object of type
'auto' with an rvalue of type 'int'` diagnostic on the second return
(because the return type of the function cannot be deduced from the first contain-errors return).

This patch suppresses this error.

Diff Detail

Unit TestsFailed

Event Timeline

hokein created this revision.Nov 27 2020, 12:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 27 2020, 12:32 AM
hokein requested review of this revision.Nov 27 2020, 12:32 AM

context: this would fix an issue in https://reviews.llvm.org/D82284.

This approach isn't the most obvious one, to me.
In the test, the lambda is invalid with auto return type, where it seems that valid with int return type and a body that contains-errors would be better.

I'm guessing this comes down to what fits most naturally in the implementation? (If you skip return statements with broken types, you need to distinguish between void lambdas and broken ones somehow).
Anyway, this decision/motivation probably also deserves a comment.

clang/lib/Sema/SemaStmt.cpp
3306

This seems to only handle lambdas (and blocks).
Is there a corresponding fix needed for C++14 auto x() { ... } ?

3331

This deserves a comment, like:

// If we've already decided this lambda is invalid, e.g. because
// we saw a `return` whose expression had an error, don't keep
// trying to deduce its return type.
hokein updated this revision to Diff 307993.Nov 27 2020, 2:15 AM
hokein marked 2 inline comments as done.

address comments and add the missing fix for C++14 auto func.

clang/lib/Sema/SemaStmt.cpp
3306

oh, right, thanks! The C++14 auo function is in a separate code path, fixed.

sammccall accepted this revision.Nov 29 2020, 2:37 AM

Great stuff, thank you!

This revision is now accepted and ready to land.Nov 29 2020, 2:37 AM
hokein retitled this revision from [clang] Improve diagnostics for auto-return-type function if the return expr contains expr. to [clang] Improve diagnostics for auto-return-type function if the return expr had an error.Nov 30 2020, 12:18 AM