by WalterBright · 3y ago · 0 comments
The trouble I've run into with that is different compilers have different warnings, sometimes they are mutually contradictory, so it is not possible to have portable code that doesn't trigger warnings in one compiler or another.
I.e. the general problem with warnings is the language gets balkanized into multiple competing and incompatible dialects.
What should happen is to carefully examine the warnings, and adopt the best into the language Standard as errors.
For example:
if (a < b < c)
That is pretty much 100% a bug in the code. Just make it illegal already. D does.For another:
for (i = 0; i < 10; ++i);
{
...
}
Long ago, an expert C programmer came to me once and said he'd spent all day trying to figure out why his loop only executed once. I pointed to the semicolon. He sighed. I put a warning for that in the compiler. I noticed that other compilers eventually did, too.In D, it's an error.
A third example:
In the Joint Fighter coding standard, it says don't use `l` as an integer suffixe, as in `124l`, because in many fonts it looks like `1241`. Solution: D does not allow `l` as a suffix. There's is no reason to support that suffix. No reason to put it in coding standard. Do the world a favor, just make it illegal. Done!
• There's a built-in way to silence specific warnings for specific portions of the code. When the compiler is wrong, instead of obfuscating the code to hide it from the compiler, you can directly mark "I know what I'm doing". Standard C can't do it, and compiler-specific #pragmas are much more clunky.
• Builds of dependencies automatically suppress lints, so builds with a -Werror equivalent won't break on someone else's code (that you may not be able to fix), just because your compiler has added new lints.
If the compiler warns that e.g. you have an unused function argument, it may be a mistake, or it may be intentional (e.g. it's a callback for something that passes the arg anyway). Then you either have ad-hoc syntax extensions or dummy expressions for convincing the compiler it's used, which may fail in another compiler that defines "used" differently, or you explicitly silence the warning. Unused is relatively easy, but there's a whole category of pedantic lints for other code smells.
D is a better language for having only a small number of warnings. It'd be even better with zero!
Right, there are definitely some C++ specific warnings for different language compat rules (around std::move IIRC) that are non-mutually-satisfyable.
This article recommends -Weverything w/ Clang. Clang developers DO NOT ENDORSE the use of -Weverything.
On the other hand the intention (assuming numerical types) is clear, if people keep doing this, what machine code is emitted by common compilers for the "best" way to express this? And how about for the idiomatic way to express it? Perhaps the insight, if it's common, is that we should provide a nice way to do this which emits efficient machine code.
Rust says "chained comparison" is forbidden, so it knows what we're going for here, and indeed it suggests you might want (a < b) && (b < c) which is how I'd write this, but of course the opposite could be faster, as perhaps could ((a+1)..c).contains(&b) and either may make more sense in context of the usage.
What on Earth does your D expression do for numeric types? And why?
In Rust I can write this only if, in fact, c is always ordered with respect to a boolean which means c is itself a boolean. Since c is a boolean then ((a<b)<c) actually means ((a>=b) && c) which er... I guess? I doubt that writing either when you really meant the other will make your intent clear.
Yes, "Is `b` between `a` and `c`?" is a natural and common question to ask. FWIW, Julia does lower `a < b < c` as a chained comparison:
julia> Meta.@lower a < b < c
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ %1 = a < b
└── goto #3 if not %1
2 ─ %3 = b < c
└── return %3
3 ─ return false
))))
That is, it turns `a < b < c` into `(a < b) && (b < c)`. You can make the chain arbitrarily (within reason) long.Of course, if a language doesn't lower chained comparisons in this way, I would much rather have a compiler error than have it silently return `(a < b) < c`.
if (condition);
{
...
}https://www.stroustrup.com/JSF-AV-rules.pdf#page17
AV Rule 14 is the one in question. Uppercase suffixes are fine, lowercase is not, and the rationale is "Readability."