Semantics of Value Dependent and Integral Constant Expressions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Semantics of Value Dependent and Integral Constant Expressions

Boris Kolpackov via cfe-dev
I have been investigating bug https://bugs.llvm.org/show_bug.cgi?id=12779, which has led me to a question regarding the semantics behind what it means to be a Value Dependent Expression and their relations to Integral Constant Expressions (ICE).

Is the constant expression: sizeof(T), where T is a template type argument, considered an ICE?  It seems to me that it is an ICE.  There is nothing in the C++11 specification which would have me believe otherwise.

The aforementioned bug triggers an assertion in Clang's frontend, specifically in Decl.cpp:VarDecl::checkInitIsICE().  That routine decides if a declaration is an ICE under the constraint that the initializer of the variable is non-value dependent.  In fact, the compiler expects to never handle the value-dependent case, as implied by the assertion.  In legal template code this assertion would/should never trigger as the template type argument would have been expanded.  But, in malformed code (e.g., during error recovery) that assertion can be triggered.

What confuses me is that the specification considers sizeof(type-id) to be a value dependent expression, even though it appears that it is an ICE.
[temp.dep.constexpr]:
Expressions of the following form are value-dependent if the unary-expression or expression is type-dependent or the type-id is dependent:
sizeof unary-expression
sizeof ( type-id )
[clip]

In the case of sizeof(T), where T is a template type argument, T is dependent; therefore, sizeof(T) is considered a value dependent constant expression.  Why is sizeof(T) disallowed from being an ICE in checkInitIsICE()? I realize that the test case referenced in the bug (comment #6) is syntactically malformed and that the compiler expects to never see this case.  

Thanks,
-Matt
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Semantics of Value Dependent and Integral Constant Expressions

Boris Kolpackov via cfe-dev
On 11/16/2017 6:49 PM, Davis, Matthew via cfe-dev wrote:
> In the case of sizeof(T), where T is a template type argument, T is dependent; therefore, sizeof(T) is considered a value dependent constant expression.  Why is sizeof(T) disallowed from being an ICE in checkInitIsICE()?

For all T (excluding VLAs, which aren't part of the standard), sizeof(T)
is an constant expression, yes.  But in general, it's impossible to tell
whether a value-dependent expression is a constant expression, so we
just don't check value-dependent expressions for the sake of
simplicity.  (Consider, for example, "sizeof(T) > 10 ? 5 :
printf("asdf");".)

-Eli

--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Semantics of Value Dependent and Integral Constant Expressions

Boris Kolpackov via cfe-dev
> From: Friedman, Eli; Sent: Friday, November 17, 2017 12:10 PM
> > On 11/16/2017 6:49 PM, Davis, Matthew via cfe-dev wrote:
> > In the case of sizeof(T), where T is a template type argument, T is dependent;
> >  therefore, sizeof(T) is considered a value dependent constant expression.  

> Why is sizeof(T) disallowed from being an ICE in checkInitIsICE()?

My reasoning is that VarDecl::checkInitIsICE() will trigger an assertion if a variable initializer is value dependent, for example: const size_t foo = sizeof(T);.  I realize that the frontend never expects to handle a value-dependent initializer in this case, but during error recovery a value-dependent initializer can reach the ICE check.

> For all T (excluding VLAs, which aren't part of the standard), sizeof(T)
> is an constant expression, yes.  But in general, it's impossible to tell
> whether a value-dependent expression is a constant expression, so we
> just don't check value-dependent expressions for the sake of
> simplicity.  (Consider, for example, "sizeof(T) > 10 ? 5 :
> printf("asdf");".)

That makes sense.  Thanks for the information,
-Matt
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev