Skip to content

Commit

Permalink
Fix diagnostic error due to auto type during copy initialization/impl…
Browse files Browse the repository at this point in the history
…icit cast
  • Loading branch information
kumarak committed May 7, 2024
1 parent 2c001aa commit 7eb761f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,19 @@ bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType,
return true;
}

// Note(kumarak): If the auto pointee type leaks into the implict cast, check
// if FromPointeeType is undeduced and generate converted type
// based on ToType. The patch avoids Diagnostic error due to
// invalid cast from auto to another type.
if (getLangOpts().LexicalTemplateInstantiation) {
if (FromPointeeType->isUndeducedAutoType()) {
ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
ToPointeeType,
ToType, Context);
return true;
}
}

return false;
}

Expand Down
10 changes: 9 additions & 1 deletion clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4160,7 +4160,15 @@ StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp,

// In C++ the return statement is handled via a copy initialization,
// the C version of which boils down to CheckSingleAssignmentConstraints.
if (!HasDependentReturnType && !RetValExp->isTypeDependent()) {

// Note(kumarak): If the Return value expression has undeduced auto types,
// don't perform the copy operation instead force cast it
// as unknown any types. Peforming copy operation on
// from undeduced expression will cause diagnostic error.

auto HasUndeducedAutoType = getLangOpts().LexicalTemplateInstantiation &&
RetValExp->getType()->isUndeducedAutoType();
if (!HasDependentReturnType && !RetValExp->isTypeDependent() && !HasUndeducedAutoType) {
// we have a non-void function with an expression, continue checking
InitializedEntity Entity =
InitializedEntity::InitializeResult(ReturnLoc, RetType);
Expand Down

0 comments on commit 7eb761f

Please sign in to comment.