cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[stmt.select.stmt.if] p1 The last sentence is a bit misleading #419

Open xmh0511 opened 11 months ago

xmh0511 commented 11 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

In the second form of if statement (the one including else), if the first substatement is also an if statement then that inner if statement shall contain an else part.

First, as discussed in other issues, "shall" is misused here, consider a concrete example:

#include <iostream>
int main(){
    if(true) if(false){std::cout<<"true\n";} else {std::cout<<"executed\n";} // print `executed`
}

The case can never match the second form of the if statement, instead, it always matches the first form of the if statement and interprets if(false){std::cout<<"true\n";} else {std::cout<<"executed\n";} as the substatement of the if statement. In other words, we couldn't construct a case, if the first substatement of the if statement is itself a complete second form of an if statement, such that the whole statement would be considered as the second form.

Suggested Resolution

At best, the sentence together with the associated note should be as a note.

languagelawyer commented 11 months ago

The case can never match the second form of the if statement, instead, it always matches the first form of the if statement

Maybe because of this "shall"?

"shall" is misused here

It is OK here, it is a requirement on an implementation how it shall disambiguate grammar

frederick-vs-ja commented 11 months ago

It is OK here, it is a requirement on an implementation how it shall disambiguate grammar

The reading is intended but hard to be parsed from the current wording.

I prefer to slightly rewrite the syntax notations:

one-branched-if-statement: attribute-specifier-seq opt if constexpr opt ( init-statement opt condition ) statement

elsable-statement: any statement except for one-branched-if-statement

selection-statement: [...] if constexpr opt ( init-statement opt condition ) elsable-statement else statement [...]

xmh0511 commented 11 months ago

It is OK here, it is a requirement on an implementation how it shall disambiguate grammar

As we discussed in other issues, violating "shall" should give a diagnosis anyway, the program is ill-formed in this situation.