https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66786
Patrick Palka <ppalka at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |ASSIGNED CC| |ppalka at gcc dot gnu.org Assignee|unassigned at gcc dot gnu.org |ppalka at gcc dot gnu.org --- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> --- Slightly more simplified test case: template <typename... T> auto list = [](T... xs) { [=](auto f) { f(xs...); }; }; int main() { list<int>; } The problem ultimately seems to lie in template_class_depth, which returns the number of enclosing templates in which the given type or decl is embedded. In particular, given the RECORD_TYPE that corresponds to the outermost lambda in the above test case, template_class_depth returns 0 instead of 1 since the TYPE_CONTEXT of this RECORD_TYPE is the file's TRANSLATION_UNIT_DECL and not the VAR_DECL so template_class_depth never sees it. That the TYPE_CONTEXT doesn't point to the enclosing VAR_DECL doesn't seem like a bug because according to the documentation, the TYPE_CONTEXT (and DECL_CONTEXT) of a tree is never expected to be a VAR_DECL. It can be a FUNCTION_DECL however, which explains why the following test case, which just replaces the enclosing variable template with a function template, compiles successfully (the TYPE_CONTEXT points to the enclosing FUNCTION_DECL and so template_class_depth appropriately returns 1): template <typename... T> auto list () { return [](T... xs) { [=](auto f) { f(xs...); }; }; } int main() { list<int> (); } So I wonder how to make template_class_depth be aware of the given type's enclosing variable template...