public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: decltype of (non-captured variable) [PR83167]
Date: Tue, 14 Nov 2023 17:43:14 -0500	[thread overview]
Message-ID: <11df54f6-a14e-468e-9559-9cc3eed6e65d@redhat.com> (raw)
In-Reply-To: <20231114161044.985367-1-ppalka@redhat.com>

On 11/14/23 11:10, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
> 
> -- >8 --
> 
> For decltype((x)) within a lambda where x is not captured, we dubiously
> require that the lambda has a capture default, unlike for decltype(x).
> This patch fixes this inconsistency; I couldn't find a justification for
> it in the standard.

The relevant passage seems to be

https://eel.is/c++draft/expr.prim#id.unqual-3

"If naming the entity from outside of an unevaluated operand within S 
would refer to an entity captured by copy in some intervening 
lambda-expression, then let E be the innermost such lambda-expression.

If there is such a lambda-expression and if P is in E's function 
parameter scope but not its parameter-declaration-clause, then the type 
of the expression is the type of a class member access expression 
([expr.ref]) naming the non-static data member that would be declared 
for such a capture in the object parameter ([dcl.fct]) of the function 
call operator of E."

In this case I guess there is no such lambda-expression because naming x 
won't refer to a capture by copy if the lambda doesn't capture anything, 
so we ignore the lambda.

Maybe refer to that in a comment?  OK with that change.

I'm surprised that it refers specifically to capture by copy, but I 
guess a capture by reference should have the same decltype as the 
captured variable?

Jason

> 	PR c++/83167
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (finish_decltype_type): If capture_decltype
> 	returns NULL_TREE, fall back to the ordinary code path.
> 	(capture_decltype): Return NULL_TREE if the lambda has no
> 	capture-default.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/lambda/lambda-decltype4.C: New test.
> ---
>   gcc/cp/semantics.cc                               |  6 +++---
>   .../g++.dg/cpp0x/lambda/lambda-decltype4.C        | 15 +++++++++++++++
>   2 files changed, 18 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 8090c71809f..6fdd6c45972 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -11732,7 +11732,8 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
>   	/* If the expression is just "this", we want the
>   	   cv-unqualified pointer for the "this" type.  */
>   	type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
> -      else
> +
> +      if (!type)
>   	{
>   	  /* Otherwise, where T is the type of e, if e is an lvalue,
>   	     decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
> @@ -12639,8 +12640,7 @@ capture_decltype (tree decl)
>       switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
>         {
>         case CPLD_NONE:
> -	error ("%qD is not captured", decl);
> -	return error_mark_node;
> +	return NULL_TREE;
>   
>         case CPLD_COPY:
>   	type = TREE_TYPE (decl);
> diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C
> new file mode 100644
> index 00000000000..0062d7b8672
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C
> @@ -0,0 +1,15 @@
> +// PR c++/83167
> +// { dg-do compile { target c++11 } }
> +
> +int main() {
> +  int x;
> +  const int y = 42;
> +
> +  [] {
> +    using ty1 = decltype((x));
> +    using ty1 = int&;
> +
> +    using ty2 = decltype((y));
> +    using ty2 = const int&;
> +  };
> +}


  reply	other threads:[~2023-11-14 22:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-14 16:10 Patrick Palka
2023-11-14 22:43 ` Jason Merrill [this message]
2023-12-01 17:32   ` Patrick Palka
2023-12-01 20:33     ` Jason Merrill
2023-12-01 22:42       ` Patrick Palka
2023-12-04  3:49         ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11df54f6-a14e-468e-9559-9cc3eed6e65d@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).