-
Notifications
You must be signed in to change notification settings - Fork 252
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
[BUG] Variable declarations can refer to themselves #1028
Comments
A presentation I saw today actually used a variant of this that was technically legal. I'm not saying that this is good code, but it is something that we might need to handle. I'm writing this from memory, but I think this is accurate.
Maybe it could be written like this:
|
Thanks! Done. But I hesitated, let me tell you why... 🗨️ In general, because I have limited time and I want to focus on adding new value beyond Cpp1, I try to avoid doing extra work to add checks for things that the Cpp1 compilers already generally diagnose consistently and well (as a warning or error), and this is one that it looks like all the major compilers do flag with a nice readable warning. So that's why I almost didn't do this... but because this is about initialization safety and I thought 'well, maybe it's not too hard,' I took a look and it worked out pretty well. Note that I had to add a little logic to not flag code like But this gave me a chance to explain why I'll say No to some requests like this, and a case to point back to in the future where I did it anyway as proof that I don't always just say No. 😁 |
Oh, and the original code at top now gets this:
|
Hey, thanks for handling this, and I totally get you (I promise I won't get mad if you say No in the future 😛). The real reason I opened the bug is because I was reminded of Bjarne's talk on safety where he said: "diagnostics are no longer enough, we need enforcement of some kind", and unfortunately, there's a lot of places where warnings are not enforced, so it seems to me like a good place for cppfront to step in and prevent you from doing that mistake ever again. |
No worries. Note that this is not necessarily without minor downsides... one use case this prevents (and we can carve out to narrowly allow if there's demand) is taking the address of the variable to register it with something that's also initializing it. I doubt that there are important existing APIs that do that, but we'll see if people encounter any... |
I think you can cover that with "you can pass it as a function's |
I thought about that, because of your example of this style... changing
The principle is that a function with an But then I realized that this would be double construction -- Using a function with an
|
I've contacted you offline about the source of this sample. |
Describe the bug
A problem as old as C itself: You are able declare a value and "initialize it" with itself, which is unsafe (and confusing).
To Reproduce
Godbolt reproducer: https://cpp2.godbolt.org/z/hxros3zM9
Additional context
We are fortunate that most compilers diagnose this, for example, Clang says:
However, IMO a warning is not enough, this should be an error because:
x
before its initialized, in C++ you have the tools to do so (like type deduction).x
that is elsewhere, this would shadow it, which is confusing and brittle since it would silently break if you rename the shadowed variable.The text was updated successfully, but these errors were encountered: