public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-8252] c++: ICE with concept defined in function [PR97536]
@ 2021-04-19 22:06 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2021-04-19 22:06 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:29d8838c5ecaf70ce552fea7639ec1f34adb2e04

commit r11-8252-g29d8838c5ecaf70ce552fea7639ec1f34adb2e04
Author: Marek Polacek <polacek@redhat.com>
Date:   Mon Apr 19 16:21:46 2021 -0400

    c++: ICE with concept defined in function [PR97536]
    
    This is an ICE-on-invalid, but I keep seeing it when reducing code so
    I'd like to fix it.  We crash on
    
      template <typename> void forward() {
        concept C = true;
      }
    
    which breaks two requirements:
    [temp.concept]/1: A concept is a template ...
    [temp.concept]/3: A concept-definition shall inhabit a namespace scope.
    
    This patch adds a test that exercises broken code and fixes the ICE
    by checking that a concept-definition is defined at namespace scope.
    
    gcc/cp/ChangeLog:
    
            PR c++/97536
            * decl.c (grokvardecl): Given an error when a concept is not defined
            at namespace scope.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/97536
            * g++.dg/concepts/diagnostic16.C: New test.

Diff:
---
 gcc/cp/decl.c                                |  6 ++++
 gcc/testsuite/g++.dg/concepts/diagnostic16.C | 45 ++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 942eb318f2c..b81de8ef934 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -10365,6 +10365,12 @@ grokvardecl (tree type,
 		    "a non-template variable cannot be %<concept%>");
           return NULL_TREE;
         }
+      else if (!at_namespace_scope_p ())
+	{
+	  error_at (declspecs->locations[ds_concept],
+		    "concept must be defined at namespace scope");
+	  return NULL_TREE;
+	}
       else
         DECL_DECLARED_CONCEPT_P (decl) = true;
       if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
diff --git a/gcc/testsuite/g++.dg/concepts/diagnostic16.C b/gcc/testsuite/g++.dg/concepts/diagnostic16.C
new file mode 100644
index 00000000000..fcba535a876
--- /dev/null
+++ b/gcc/testsuite/g++.dg/concepts/diagnostic16.C
@@ -0,0 +1,45 @@
+// PR c++/97536
+// { dg-do compile { target concepts } }
+
+template<typename>
+concept C1 = true;
+
+concept C2 = true; // { dg-error "non-template variable cannot be .concept." }
+// { dg-error "concept definition syntax is" "" { target *-*-* } .-1 }
+
+template<typename>
+void fn1 ()
+{
+  concept bar = true; // { dg-error "concept must be defined at namespace scope" }
+// { dg-error "concept definition syntax is" "" { target *-*-* } .-1 }
+}
+
+void fn2 ()
+{
+  concept bar = true; // { dg-error "non-template variable cannot be .concept." }
+// { dg-error "concept definition syntax is" "" { target *-*-* } .-1 }
+}
+
+template<typename>
+void fn3 ()
+{
+  template<typename> // { dg-error "template declaration cannot appear at block scope" }
+  concept bar = true;
+}
+
+void fn4 ()
+{
+  template<typename> // { dg-error "template declaration cannot appear at block scope" }
+  concept bar = true;
+}
+
+void fn5 ()
+{
+  C1 auto x = 42;
+}
+
+template<typename>
+void fn6 ()
+{
+  C1 auto x = 42;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-19 22:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-19 22:06 [gcc r11-8252] c++: ICE with concept defined in function [PR97536] Marek Polacek

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).