]> granicus.if.org Git - clang/commitdiff
Always use a function's decl context when building default arguments. Fixes http...
authorNico Weber <nicolasweber@gmx.de>
Mon, 29 Nov 2010 18:19:25 +0000 (18:19 +0000)
committerNico Weber <nicolasweber@gmx.de>
Mon, 29 Nov 2010 18:19:25 +0000 (18:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120299 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/SemaCXX/friend.cpp

index 087314fbcba7e4e45c598073a8a84dae6a1911c8..e8025409bd6ac53c86407f2c364c5bb35e5e7ee8 100644 (file)
@@ -3839,8 +3839,8 @@ ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
 }
 
 ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc,
-                                                    FunctionDecl *FD,
-                                                    ParmVarDecl *Param) {
+                                        FunctionDecl *FD,
+                                        ParmVarDecl *Param) {
   if (Param->hasUnparsedDefaultArg()) {
     Diag(CallLoc,
           diag::err_use_of_default_argument_to_function_declared_later) <<
@@ -3857,12 +3857,20 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc,
     MultiLevelTemplateArgumentList ArgList
       = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true);
 
-    std::pair<const TemplateArgument *, unsigned> Innermost 
+    std::pair<const TemplateArgument *, unsigned> Innermost
       = ArgList.getInnermost();
     InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first,
                                Innermost.second);
 
-    ExprResult Result = SubstExpr(UninstExpr, ArgList);
+    ExprResult Result;
+    {
+      // C++ [dcl.fct.default]p5:
+      //   The names in the [default argument] expression are bound, and
+      //   the semantic constraints are checked, at the point where the
+      //   default argument expression appears.
+      ContextRAII SavedContext(*this, FD->getDeclContext());
+      Result = SubstExpr(UninstExpr, ArgList);
+    }
     if (Result.isInvalid())
       return ExprError();
 
index 939d3ae456879f3a62ee3ba09193b7015779f463..515edfd949f227ff5a2f9c751beb47a5bc3c42d9 100644 (file)
@@ -79,3 +79,36 @@ namespace test5 {
 
   struct B { friend void B(); };
 }
+
+// PR8479
+namespace test6_1 {
+  class A {
+   public:
+   private:
+    friend class vectorA;
+    A() {}
+  };
+  class vectorA {
+   public:
+    vectorA(int i, const A& t = A()) {}
+  };
+  void f() {
+    vectorA v(1);
+  }
+}
+namespace test6_2 {
+  template<class T>
+  class vector {
+   public:
+    vector(int i, const T& t = T()) {}
+  };
+  class A {
+   public:
+   private:
+    friend class vector<A>;
+    A() {}
+  };
+  void f() {
+    vector<A> v(1);
+  }
+}