diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 6a2cf2c123fbd600f0c027eb00c0e306df7f2089..2345a18becc1160b9d12f3d88cccb66c8917373c 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -9918,16 +9918,27 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
 	 template.  */
 
       /* Shortcut looking up the current class scope again.  */
-      if (current_class_type)
-	if (tree ti = CLASSTYPE_TEMPLATE_INFO (current_class_type))
+      for (tree cur = current_nonlambda_class_type ();
+	   cur != NULL_TREE;
+	   cur = get_containing_scope (cur))
+	{
+	  if (!CLASS_TYPE_P (cur))
+	    continue;
+
+	  tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
+	  if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
+	    break;
+
 	  if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
 	      && comp_template_args (arglist, TI_ARGS (ti)))
-	    return current_class_type;
+	    return cur;
+	}
 
       /* Calculate the BOUND_ARGS.  These will be the args that are
 	 actually tsubst'd into the definition to create the
 	 instantiation.  */
-      arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
+      if (PRIMARY_TEMPLATE_P (gen_tmpl))
+	arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
 
       if (arglist == error_mark_node)
 	/* We were unable to bind the arguments.  */
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class57.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class57.C
new file mode 100644
index 0000000000000000000000000000000000000000..1520fc59f6478056a7e1b3299b4608fd54ea7318
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class57.C
@@ -0,0 +1,25 @@
+// PR c++/110122
+// { dg-do compile { target c++20 } }
+
+struct Foo {
+  Foo() = default;
+  Foo(const Foo&) = delete;
+};
+
+template<Foo V>
+struct A {
+  A() {
+    [] { A a; }();
+    [this] { this; }();
+  }
+
+  struct B {
+    B() {
+      [] { A a; }();
+      [this] { this; }();
+    }
+  };
+};
+
+A<Foo{}> a;
+A<Foo{}>::B b;
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C
new file mode 100644
index 0000000000000000000000000000000000000000..5bb335ca9e7781bb4e383adccb062445cddedd33
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C
@@ -0,0 +1,20 @@
+// PR c++/110122
+// { dg-do compile { target c++20 } }
+
+struct Foo {
+  Foo() = default;
+  constexpr Foo(const Foo&) { }
+};
+
+struct Bar {
+  Foo _;
+};
+
+template<Bar V>
+struct A {
+  A() {
+    [](auto){ A<V> d; }(0); // { dg-bogus "used before its definition" }
+  };
+};
+
+A<Bar{}> a;