2012-07-12  Jason Merrill  <jason@redhat.com>
 
+       * pt.c (instantiate_decl): Check typedefs access here.
+       (instantiate_template_1): Not here.
+
        * pt.c (deduction_tsubst_fntype): Just suppress access checking.
        (instantiate_template_1): Set DECL_TI_TEMPLATE before access checking.
        (push_deduction_access_scope, pop_deduction_access_scope): Remove.
 
 
 /* Perform (or defer) access check for typedefs that were referenced
    from within the template TMPL code.
-   This is a subroutine of instantiate_template and instantiate_class_template.
+   This is a subroutine of instantiate_decl and instantiate_class_template.
    TMPL is the template to consider and TARGS is the list of arguments of
    that template.  */
 
   /* Now we know the specialization, compute access previously
      deferred.  */
   push_access_scope (fndecl);
-
-  /* Some typedefs referenced from within the template code need to be access
-     checked at template instantiation time, i.e now. These types were
-     added to the template at parsing time. Let's get those and perfom
-     the acces checks then.  */
-  perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
   perform_deferred_access_checks ();
   pop_access_scope (fndecl);
   pop_deferring_access_checks ();
       /* Set up context.  */
       start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
 
+      /* Some typedefs referenced from within the template code need to be
+        access checked at template instantiation time, i.e now. These
+        types were added to the template at parsing time. Let's get those
+        and perform the access checks then.  */
+      perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
+                                    gen_args);
+
       /* Create substitution entries for the parameters.  */
       subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
       tmpl_parm = DECL_ARGUMENTS (subst_decl);
 
 /* If we are currently parsing a template and we encountered a typedef
    TYPEDEF_DECL that is being accessed though CONTEXT, this function
    adds the typedef to a list tied to the current template.
-   At tempate instantiatin time, that list is walked and access check
+   At template instantiation time, that list is walked and access check
    performed for each typedef.
    LOCATION is the location of the usage point of TYPEDEF_DECL.  */