What is a semantic rule?

54 views
Skip to first unread message

Language Lawyer

unread,
Aug 28, 2018, 9:21:13 PM8/28/18
to std-dis...@isocpp.org
Is it any rule speaking about values of objects and relations between them?

For example, let's take a look at [expr.add]/4:
When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand.
If the expression P points to element x[i] of an array object x with n elements, the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) element x[i+j] if 0≤i+j≤n; otherwise, the behavior is undefined.

Is this requirement on the value of a pointer and the value of an integer expression a semantic rule?

hubert.rein...@gmail.com

unread,
Aug 29, 2018, 10:34:39 AM8/29/18
to ISO C++ Standard - Discussion
It would help to understand the context of where this question is coming from, because the answer to the question may be designed to be irrelevant through reaching the same outcome regardless of the answer. It is clear here that there is no diagnosable rule involved. It is also clear that undefined behaviour is in play.

Language Lawyer

unread,
Aug 29, 2018, 11:02:44 AM8/29/18
to std-dis...@isocpp.org
Thank you for the request to provide the context.
I reread the sentence and realized that I had overlooked something.
Namely, the Note to the definition of undefined behavior says:
"Undefined behavior may be expected when this document omits any explicit
definition of behavior or when a program uses an erroneous construct or
erroneous data."

I had overlooked the second part and was surprised that every violation
of a semantic rule not explicitly marked as UB (or for which no diagnostics
is required) has to be diagnosed.
Now I see that this is not the case.


Anyway, it would be great to have an explanation what semantic rules are, generally.

hubert.rein...@gmail.com

unread,
Aug 29, 2018, 11:36:54 AM8/29/18
to ISO C++ Standard - Discussion
On Wednesday, August 29, 2018 at 11:02:44 AM UTC-4, Language Lawyer wrote:
Thank you for the request to provide the context.
I reread the sentence and realized that I had overlooked something.
Namely, the Note to the definition of undefined behavior says:
"Undefined behavior may be expected when this document omits any explicit
definition of behavior or when a program uses an erroneous construct or
erroneous data."
The sentence verges upon being meaningless. Some entity (in the general sense) is permitted to expect undefined behaviour; however, such expectations are not required to be fulfilled.
 

I had overlooked the second part and was surprised that every violation
of a semantic rule not explicitly marked as UB (or for which no diagnostics
is required) has to be diagnosed.
Now I see that this is not the case.
The key question is indeed whether a diagnostic is required. The convention used in the Standard is to use "shall" in order to form a requirement that affects the well-formedness of a program.



Anyway, it would be great to have an explanation what semantic rules are, generally.
 I think that the wording is a bit lax here and that there could be a distinction made between semantic rules and semantic requirements as terms of art. Semantic requirements affect the well-formedness of a program. A program may be well-formed with undefined behaviour (and the definition of well-formed program also admits programs that are ill-formed...). Semantic rules may be read as anywhere in the spectrum from "semantic requirements" to "all of the statements made with regards to semantics, including those written as statements of fact regarding C++". The latter interpretation seems to see some usage with regards to C in the C Standard.

Language Lawyer

unread,
Aug 29, 2018, 12:31:49 PM8/29/18
to std-dis...@isocpp.org
On 29/08/18 20:36, hubert.rein...@gmail.com wrote:
> On Wednesday, August 29, 2018 at 11:02:44 AM UTC-4, Language Lawyer wrote:
>>
>> Thank you for the request to provide the context.
>> I reread the sentence and realized that I had overlooked something.
>> Namely, the Note to the definition of undefined behavior says:
>> "Undefined behavior may be expected when this document omits any explicit
>> definition of behavior or when a program uses an erroneous construct or
>> erroneous data."
>>
> The sentence verges upon being meaningless. Some entity (in the general
> sense) is permitted to expect undefined behaviour; however, such
> expectations are not required to be fulfilled.
>
>
>>
>> I had overlooked the second part and was surprised that every violation
>> of a semantic rule not explicitly marked as UB (or for which no
>> diagnostics
>> is required) has to be diagnosed.
>> Now I see that this is not the case.
>>
> The key question is indeed whether a diagnostic is required. The convention
> used in the Standard is to use "shall" in order to form a requirement that
> affects the well-formedness of a program.

[dcl.ref]/5: A reference shall be initialized to refer to a valid object or function.

First of all, it is not 100% clear for me what "a valid object" is, so I've opened an editorial issue (maybe, it is well-known what does it mean, so I'll close it).
But the question is: is it a diagnosable semantic rule?

The Note following the sentence suggests that probably it is not:
[Note: In particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by indirection through a null pointer, which causes undefined behavior.
As described in [class.bit], a reference cannot be bound directly to a bit-field. — end note]

I suppose here "shall" is used to put a requirement on a well-defined, not a well-formed program.

Johannes Sixt

unread,
Aug 29, 2018, 2:04:08 PM8/29/18
to std-dis...@isocpp.org, Language Lawyer
Am 29.08.2018 um 17:02 schrieb Language Lawyer:
> Anyway, it would be great to have an explanation what semantic rules
> are, generally.

In my book, everything that cannot be expressed in LR(0) or LALR(1) or
LL(1) syntax rules (pick your favorite, they are equivalent, AFAIR) is a
semantic rule. That is basically everything written in the Standard
except for the grammar productions.

hubert.rein...@gmail.com

unread,
Aug 29, 2018, 4:32:50 PM8/29/18
to ISO C++ Standard - Discussion
On Wednesday, August 29, 2018 at 12:31:49 PM UTC-4, Language Lawyer wrote:
On 29/08/18 20:36, hubert.rein...@gmail.com wrote:
> On Wednesday, August 29, 2018 at 11:02:44 AM UTC-4, Language Lawyer wrote:
>>
>> Thank you for the request to provide the context.
>> I reread the sentence and realized that I had overlooked something.
>> Namely, the Note to the definition of undefined behavior says:
>> "Undefined behavior may be expected when this document omits any explicit
>> definition of behavior or when a program uses an erroneous construct or
>> erroneous data."
>>
> The sentence verges upon being meaningless. Some entity (in the general
> sense) is permitted to expect undefined behaviour; however, such
> expectations are not required to be fulfilled.
>  
>
>>
>> I had overlooked the second part and was surprised that every violation
>> of a semantic rule not explicitly marked as UB (or for which no
>> diagnostics
>> is required) has to be diagnosed.
>> Now I see that this is not the case.
>>
> The key question is indeed whether a diagnostic is required. The convention
> used in the Standard is to use "shall" in order to form a requirement that
> affects the well-formedness of a program.

[dcl.ref]/5: A reference shall be initialized to refer to a valid object or function.

First of all, it is not 100% clear for me what "a valid object" is, so I've opened an editorial issue (maybe, it is well-known what does it mean, so I'll close it).
But the question is: is it a diagnosable semantic rule?
I think that the current wording conventions say "yes" and that they consider this use of "shall" to be erroneous. Would you file an editorial issue for this one?
 

Language Lawyer

unread,
Aug 30, 2018, 1:23:03 AM8/30/18
to std-dis...@isocpp.org
On 30/08/18 01:32, hubert.rein...@gmail.com wrote:
> On Wednesday, August 29, 2018 at 12:31:49 PM UTC-4, Language Lawyer wrote:
>>
I've added it to the issue about what does "valid object" in this sentence means.
Reply all
Reply to author
Forward
0 new messages