From 893f955321cdf49dd0ceaf4ff821632e9b265000 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 30 Sep 2009 21:23:30 +0000 Subject: [PATCH] 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 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaDeclCXX.cpp | 11 ++++++++--- test/SemaCXX/decl-init-ref.cpp | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 06e1516686..b5672fd7ca 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -474,6 +474,8 @@ def err_destructor_name : Error< // 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 223662afce..1966aebca2 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -3703,9 +3703,14 @@ Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, 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 a87c0efae2..9fb3f171d6 100644 --- a/test/SemaCXX/decl-init-ref.cpp +++ b/test/SemaCXX/decl-init-ref.cpp @@ -18,6 +18,8 @@ class B : public BASE , public BASE1 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'}} -- 2.40.0