Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -5820,6 +5820,8 @@ "taking the address of a destructor">; def err_typecheck_unary_expr : Error< "invalid argument type %0 to unary expression">; +def err_unsignedtypecheck_unary_minus : Error< + "unary minus operator applied to type %0, result value is still positive">; def err_typecheck_indirection_requires_pointer : Error< "indirection requires pointer operand (%0 invalid)">; def ext_typecheck_indirection_through_void_pointer : ExtWarn< Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp +++ lib/Sema/SemaExpr.cpp @@ -12646,8 +12646,12 @@ resultType = Input.get()->getType(); if (resultType->isDependentType()) break; - if (resultType->isArithmeticType()) // C99 6.5.3.3p1 + if (resultType->isArithmeticType()) { // C99 6.5.3.3p1 + if (Opc == UO_Minus && resultType->isUnsignedIntegerType()) + return ExprError(Diag(OpLoc, diag::err_unsignedtypecheck_unary_minus) + << resultType << Input.get()->getSourceRange()); break; + } else if (resultType->isVectorType() && // The z vector extensions don't allow + or - with bool vectors. (!Context.getLangOpts().ZVector || Index: test/Sema/unary-minus-unsigned.c =================================================================== --- test/Sema/unary-minus-unsigned.c +++ test/Sema/unary-minus-unsigned.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only + +void test1(void) { + unsigned int a = 1; + unsigned long b = 2; + unsigned long long c = 1; + + unsigned int a2 = -a; // expected-error {{unary minus operator applied to type 'unsigned int', result value is still positive}} + unsigned long b2 = -b; // expected-error {{unary minus operator applied to type 'unsigned long', result value is still positive}} + unsigned long long c2 = -c; // expected-error {{unary minus operator applied to type 'unsigned long long', result value is still positive}} + }