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

[clang] New static_assert message is sometimes redundant #57906

Closed
cjdb opened this issue Sep 22, 2022 · 9 comments
Closed

[clang] New static_assert message is sometimes redundant #57906

cjdb opened this issue Sep 22, 2022 · 9 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer enhancement Improving things as opposed to bug fixing, e.g. new or missing feature good first issue https://github.com/llvm/llvm-project/contribute

Comments

@cjdb
Copy link
Contributor

cjdb commented Sep 22, 2022

The new static_assert message is great, there are some simple messages where an expansion isn't particularly helpful.

For example

constexpr auto is_gitlab = false;
constexpr auto is_weekend = false;

static_assert(is_gitlab or is_weekend);

will give us

<source>:4:1: error: static assertion failed due to requirement 'is_gitlab || is_weekend'
static_assert(is_gitlab or is_weekend);
^             ~~~~~~~~~~~~~~~~~~~~~~~
<source>:4:25: note: expression evaluates to 'false || false'
static_assert(is_gitlab or is_weekend);
              ~~~~~~~~~~^~~~~~~~~~~~~

The note isn't necessary, since the error implies that the expression will be false or false. I noticed that if we change the expression to a conjunction, we get a simpler message, so it looks like the machinery might already be present.

<source>:4:1: error: static assertion failed due to requirement 'is_gitlab'
static_assert(is_gitlab and is_weekend);
^             ~~~~~~~~~

We should---on a best effort basis---aim to eliminate expansions that don't add value to minimise user frustration (this will give us slightly more room to have longer diagnostics elsewhere).

@cjdb cjdb added enhancement Improving things as opposed to bug fixing, e.g. new or missing feature good first issue https://github.com/llvm/llvm-project/contribute clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 22, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 22, 2022

@llvm/issue-subscribers-good-first-issue

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 22, 2022

@llvm/issue-subscribers-clang-frontend

@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Sep 22, 2022
@cjdb cjdb added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Sep 22, 2022
@tbaederr
Copy link
Contributor

The code already tries to minimize unnecessary notes, but this seems to be missing.

The relevant function is Sema::DiagnoseStaticAssertDetails() in clang/lib/Sema/SemaDeclCXX.cpp.

@EugeneZelenko EugeneZelenko removed the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Oct 21, 2022
@Fznamznon
Copy link
Contributor

I poked around this one little bit and the fact that the note is not emitted for conjunction seems to be a coincidence. In case of conjunction DiagnoseStaticAssertDetails gets not a binary expression as an input but just a DeclRefExpr referencing on of the operands and early exits. I guess it is just the way findFailedBooleanCondition works whose output is then passed to DiagnoseStaticAssertDetails , it searches for a failed condition of a conjunction so in case of other boolean expressions like or, xor and etc are passed to findFailedBooleanCondition it returns the whole expression.
I'm thinking we could try check if operands of the binary expression is constexpr variables that are initialized by boolean literals. WDYT?

@Krishna-13-cyber
Copy link
Contributor

Krishna-13-cyber commented Mar 9, 2023

Hello @tbaederr @cjdb ,
I was just working on this issue, there is a specific condition which is mentioned in the DiagnoseStaticAssertDetails so if we remove that case or just return nothing, it wont give the note that is currently being emitted/displayed.
Screenshot from 2023-03-09 13-17-22
Please let me know if this is the correct approach!

@tbaederr
Copy link
Contributor

I think that is the output @cjdb expected, but we can't judge if your approach is the right one without seeing a patch.

@Unique-Usman
Copy link
Contributor

@Krishna-13-cyber , are you still working on this?

@Krishna-13-cyber
Copy link
Contributor

Yes, I have solved this and has been merged recently!

@tbaederr
Copy link
Contributor

Closing this then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer enhancement Improving things as opposed to bug fixing, e.g. new or missing feature good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

No branches or pull requests

7 participants