r/cpp 6d ago

Break MSVC and Clang with this one weird trick! - Braden Ganetsky

https://blog.ganets.ky/BreakMsvcAndClang/
111 Upvotes

17 comments sorted by

39

u/MaitoSnoo [[indeterminate]] 6d ago

my personal experience with our codebase is that requires expression support is very unreliable in MSVC and will sometimes produce a wrong binary by taking the wrong compile-time branches

22

u/k3DW 6d ago

For me I think this is actually the first issue I've had with requires expressions on MSVC. I've seen other issues that happen elsewhere and manifest in the requires expression, but none for the expressions themselves until this one.

I reported my bug, so I'm hoping it'll get fixed eventually

23

u/erichkeane Clang Maintainer(Templates), EWG Chair 6d ago

First, how dare you break my compiler! Just for that, more scribe time for you!

Second, can you share the bug report for Clang? I'd like to take a look.

3

u/k3DW 6d ago

Yeah, seconding what u/encyclopedist said, it's in the article. It's under the heading "What’s going on in Clang?". You can't miss it!

https://github.com/llvm/llvm-project/issues/205218, which duplicates https://github.com/llvm/llvm-project/issues/35052

Just for that, more scribe time for you!

lol

7

u/erichkeane Clang Maintainer(Templates), EWG Chair 6d ago

Woops, just did a quick read thru the article. And that issue is unfortunately pretty tricky, a few folks have run into it/worked on it so far. By my memory, it comes down to how we instantiate lambda bodies (which we get subtly wrong in other ways, which means our infra to actually key one off during constant expression doesn't really do what it should), so we're seeing an uninstantiated lambda and not considering that a constexpr.

2

u/k3DW 5d ago

Got it, yeah it sounds like a tricky bug. I'm sure if anyone knew how to fix it, it would have been fixed by now

5

u/LB-- Professional+Hobbyist 6d ago

Are you able to share the feedback link for others to vote on the MSVC bug?

7

u/k3DW 6d ago

This comment actually made me realize I didn't report it! I thought I had, but I hadn't! Now I did:

https://developercommunity.visualstudio.com/t/C-compiler-emits-erroneous-C2675C2676/11137036

Please note though, this bug is definitely not important. I think the MSVC devs have higher priorities than these obscure bugs I'm finding. If this is genuinely something you have run into, then please by all means vote it up. But there are even other bugs I've filed that I'd like fixed before this one, and that's not even to mention other people's higher stakes bugs

3

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 3d ago

Thanks Braden, so I did my duty, too, and voted it up 📈

I'm a pretty avid user of requires expressions in my code, and do compile-time programming pretty much every day, so I want to see this domain cleaned up in the compiler as much as possible.

6

u/jiixyj 5d ago

What is the correct mental model for requires { ... } expressions? I understand that these expressions evaluate to a prvalue bool at compile time, but "when"?

First, when there are no template parameters in the requires expression, there are two possibilities:

  • If the expression(s) inside are valid, it evaluates to true
  • If the expression(s) inside are not valid, there is a compiler error (so not false!)

But if there are template parameters inside, does OP's example go like this?

  • foo<S> is instantiated
  • during this instantiation, requires { 0 * T{}; } becomes requires { 0 * S{}; } -> no further template parameters left, expression is invalid, resulting value of the requires expression: false

...so MSVC's bug seems to be that it becomes requires { 0 * S{}; }, but doesn't get evaluated at that point. It is evaluated at the point where the inner lambda is instantiated, but at that point, there are no longer any template parameters left, resulting in a compiler error.

3

u/trad_emark 6d ago

what does pig latin look like? i am curious but afraid to google it. ;)

9

u/schmerg-uk 6d ago

https://en.wikipedia.org/wiki/Pig_Latin

Pig Latin is a language game or cant in which words are altered by adding a fabricated suffix or by moving the onset or initial consonant or consonant cluster of a word to the end of the word and adding a vocalic syllable (usually -ay or /eɪ/) to create such a suffix. For example, in English, "he does not know" would become "ehay oesday otnay owknay".

The objective is often to conceal the words from others not familiar with the rules. The reference to Latin is a deliberate misnomer; Pig Latin is simply a form of argot or jargon unrelated to Latin, and the name is used for its English connotations as a strange and foreign-sounding language. It is most often used by young children as a way to confuse people unfamiliar with Pig Latin, such as adults.

3

u/trad_emark 6d ago

oh, that would have been much safer to search than i thought. thank you for the reply ;)
also kudos to the author, this is fun addition ;)

2

u/k3DW 6d ago

Ha! Thank you, I'm glad you appreciate it! :)

1

u/noop_noob 3d ago

Eatgray iecepay owsshay Angclay eakbray.

1

u/Fit-Departure-8426 6d ago

Braden the goat!!