Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: invalid argument type 'T *' to unary expression #93331

Closed
dcci opened this issue May 24, 2024 · 2 comments
Closed

error: invalid argument type 'T *' to unary expression #93331

dcci opened this issue May 24, 2024 · 2 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@dcci
Copy link
Member

dcci commented May 24, 2024

After #90500 -- some of our internal code stopped compiling.

$ ~/llvm-build-upstream/bin/clang /tmp/Out.cpp
/tmp/Out.cpp:3:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
    3 | #pragma once
      |         ^
/tmp/Out.cpp:65:14: error: invalid argument type 'T *' to unary expression
   65 |     *item_ = -item_;
      |              ^~~~~~
1 warning and 1 error generated.

File attached.
repro.cpp.txt

cc: @erichkeane / @sdkrystian

I don't exclude that our code might be incorrect, but I wonder why this only applies to operator- (and not operator+)

@dcci dcci added the clang Clang issues not falling into any other category label May 24, 2024
@erichkeane
Copy link
Collaborator

This is a case where the source is wrong, unary minus on a pointer is ill formed, but unary plus is not.

See:
https://eel.is/c++draft/expr.unary#op-7

The operand of the unary + operator shall be a prvalue of arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument.  Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.

And:
https://eel.is/c++draft/expr.unary#op-8

The operand of the unary - operator shall be a prvalue of arithmetic or unscoped enumeration type and the result is the negative of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the promoted operand.
The type of the result is the type of the promoted operand.

Note the unary+ permits arithmetic, unscoped enum, and pointer type, but unary- is arithmetic or unscoped enum only. Effectively, the semantics of a negation operator on a pointer is not defined, and has no real reasonable way to define it,
whereas the unary + operator has the effect of 'do nothing', which is reasonable.

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels May 24, 2024
@llvmbot
Copy link
Member

llvmbot commented May 24, 2024

@llvm/issue-subscribers-clang-frontend

Author: Davide Italiano (dcci)

After https://github.com//pull/90500 -- some of our internal code stopped compiling.
$ ~/llvm-build-upstream/bin/clang /tmp/Out.cpp
/tmp/Out.cpp:3:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
    3 | #pragma once
      |         ^
/tmp/Out.cpp:65:14: error: invalid argument type 'T *' to unary expression
   65 |     *item_ = -item_;
      |              ^~~~~~
1 warning and 1 error generated.

File attached.
repro.cpp.txt

cc: @erichkeane / @sdkrystian

I don't exclude that our code might be incorrect, but I wonder why this only applies to operator- (and not operator+)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants