Issue good diagnostics when initializing a refernce type with
a bad initializer. Fixes pr4274.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83169 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 06e1516..b5672fd 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -474,6 +474,8 @@
// C++ initialization
def err_lvalue_to_rvalue_ref : Error<"rvalue reference cannot bind to lvalue">;
+def err_invalid_initialization : Error<
+"invalid initialization of reference of type %0 from expression of type %1">;
def err_lvalue_to_rvalue_ambig_ref : Error<"rvalue reference cannot bind to lvalue "
"due to multiple conversion functions">;
// FIXME: passing in an English string as %1!
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 223662a..1966aeb 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -3703,9 +3703,14 @@
Diag(Func->getLocation(), diag::err_ovl_candidate);
}
}
- else
- Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref)
- << Init->getSourceRange();
+ else {
+ if (isRValRef)
+ Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref)
+ << Init->getSourceRange();
+ else
+ Diag(DeclLoc, diag::err_invalid_initialization)
+ << DeclType << Init->getType() << Init->getSourceRange();
+ }
}
return badConversion;
}
diff --git a/test/SemaCXX/decl-init-ref.cpp b/test/SemaCXX/decl-init-ref.cpp
index a87c0ef..9fb3f17 100644
--- a/test/SemaCXX/decl-init-ref.cpp
+++ b/test/SemaCXX/decl-init-ref.cpp
@@ -18,6 +18,8 @@
extern B f();
+const int& ri = (void)0; // expected-error {{invalid initialization of reference of type 'int const &' from expression of type 'void'}}
+
int main() {
const A& rca = f(); // expected-error {{rvalue reference cannot bind to lvalue due to multiple conversion functions}}
A& ra = f(); // expected-error {{non-const lvalue reference to type 'struct A' cannot be initialized with a temporary of type 'class B'}}