cplusplus / CWG

Core Working Group
24 stars 7 forks source link

[stmt.if] p2 How does semantical and syntatical analysis peform for the discarded statement of a specialization #274

Open xmh0511 opened 1 year ago

xmh0511 commented 1 year ago

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

struct A{};
template<class T>
void fun(T t){
   if constexpr(std::is_same_v<T, A>){
       t;
  }else{
      t + t;
  }
}
fun(A{});

[stmt.if] p2 says, the statement in else is the discard statement, and it is not instantiated. However, the validity of the discard statement of the specialization is not regulated by [temp.res.general] p6 because p6 applies to template. It is underspecified how semantical and syntactical analysis will perform to the discard statement of an instantiated specialization.

Suggested Resolution

We may say, all discarded statements will be ignored.

jensmaurer commented 1 year ago

The template consists of the entirety of the function body (including the non-instantiated part). Thus, [temp.res.general] p6 does apply, and p6.1 expressly refers to "constexpr if".

It is expressly not desired that the non-taken branch of an "if constexpr" can contain token soup (i.e. lexical garbage).

In your example above, a valid instantiation would be for T=int, so [temp.res.general] p6 is satisfied, I think.

xmh0511 commented 1 year ago

The template consists of the entirety of the function body (including the non-instantiated part).

The specialization is not a template. Moreover, [temp.res.general] p6 says

The validity of a template may be checked prior to any instantiation. The program is ill-formed, no diagnostic required, if:

This implies that [temp.res.general] p6 does not apply to specialization since specialization is produced after the initialization and it is not a template.

For a specialization that encloses a discarded statement, [stmt.if] p2 only says the statement is not instantiated, it does not specify how the analysis will do on these discarded statements.

frederick-vs-ja commented 1 year ago

Is the current wording insufficient to express that discarded statements are not analyzed during instantiation?

xmh0511 commented 1 year ago

Is the current wording insufficient to express that discarded statements are not analyzed during instantiation?

The validity of a template may be checked prior to any instantiation.

The current wording just talks about how the validity would be checked before any instantiation. Moreover, the validity is just in terms of the template. Instantiated specialization enclosing the discarded statement is not a template.

jensmaurer commented 1 year ago

[temp.res.general] p6 checks the (entire) template before any instantiation. As you correctly observe, there is no further check after the instantiation of the template at that level. Since discarded statements are not instantiated, there cannot possibly be any additional checking depending on the specific values of the template parameters (exactly because "t + t" is not instantiated, so we can't know whether it's valid for a particular T or not).

Can you make an argument why "t + t" in your example should yield a diagnostic under the current rules? If not, I think we're good.

xmh0511 commented 1 year ago

The status quo is we didn't say the uninstantiated part in a specialization will undergo analysis, as well, we also didn't say the instantiated part will undergo analysis except for the name lookup.

If the name is dependent (as specified in [temp.dep]), it is looked up for each specialization (after substitution) because the lookup depends on a template parameter.

jensmaurer commented 1 year ago

So, you concern is not about "if constexpr", but your concerns is about specifying that we perform full semantic analysis for each specialization that has been instantiated? (And thus, conversely, no semantic analysis beyond [temp.res.general] p6 is done without instantiation?)

xmh0511 commented 1 year ago

So, you concern is not about "if constexpr", but your concerns is about specifying that we perform full semantic analysis for each specialization that has been instantiated? (And thus, conversely, no semantic analysis beyond [temp.res.general] p6 is done without instantiation?)

Yes, I think "if constexpr" is a subset of the issue.