From: Anders Carlsson Date: Fri, 16 Oct 2009 01:44:21 +0000 (+0000) Subject: The result type of logical || and && is bool in C++. Fixes PR5206. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04905014fd854979ac12fc31a2b25fca37a93eb4;p=clang The result type of logical || and && is bool in C++. Fixes PR5206. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84231 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index a946500660..d818a5027e 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -4812,9 +4812,16 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] UsualUnaryConversions(lex); UsualUnaryConversions(rex); - if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) - return Context.IntTy; - return InvalidOperands(Loc, lex, rex); + if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) + return InvalidOperands(Loc, lex, rex); + + if (Context.getLangOptions().CPlusPlus) { + // C++ [expr.log.and]p2 + // C++ [expr.log.or]p2 + return Context.BoolTy; + } + + return Context.IntTy; } /// IsReadonlyProperty - Verify that otherwise a valid l-value expression diff --git a/test/SemaCXX/bool.cpp b/test/SemaCXX/bool.cpp index bc44c73d8c..259c09c6cb 100644 --- a/test/SemaCXX/bool.cpp +++ b/test/SemaCXX/bool.cpp @@ -16,3 +16,15 @@ void test(bool b) bool *b1 = (int *)0; // expected-error{{expected 'bool *'}} } + +// static_assert_arg_is_bool(x) compiles only if x is a bool. +template +void static_assert_arg_is_bool(T x) { + bool* p = &x; +} + +void test2() { + int n = 2; + static_assert_arg_is_bool(n && 4); + static_assert_arg_is_bool(n || 5); +}