Skip to content

Commit

Permalink
[C++20] [Modules] Handle reachability for deduction guide
Browse files Browse the repository at this point in the history
Previously, we forget to handle reachability for deduction guide.
The deduction guide is a hint to the compiler. And the deduction guide
should be able to use if the corresponding template decl is reachable.
  • Loading branch information
ChuanqiXu9 committed Jul 18, 2022
1 parent 11079e8 commit 4983fdf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,13 @@ bool LookupResult::isAvailableForLookup(Sema &SemaRef, NamedDecl *ND) {
if (isVisible(SemaRef, ND))
return true;

// Deduction guide lives in namespace scope generally, but it is just a
// hint to the compilers. What we actually lookup for is the generated member
// of the corresponding template. So it is sufficient to check the
// reachability of the template decl.
if (auto *DeductionGuide = ND->getDeclName().getCXXDeductionGuideTemplate())
return SemaRef.hasReachableDefinition(DeductionGuide);

auto *DC = ND->getDeclContext();
// If ND is not visible and it is at namespace scope, it shouldn't be found
// by name lookup.
Expand Down
30 changes: 30 additions & 0 deletions clang/test/Modules/deduction-guide.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/Templ.cppm -emit-module-interface -o %t/Templ.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -verify -fsyntax-only

//--- foo.h
template <typename T>
class Templ {
public:
Templ(T a) {}
};

template<typename T>
Templ(T t) -> Templ<T>;

//--- Templ.cppm
module;
#include "foo.h"
export module Templ;
export using ::Templ;

//--- Use.cpp
// expected-no-diagnostics
import Templ;
void func() {
Templ t(5);
}

25 changes: 25 additions & 0 deletions clang/test/Modules/deduction-guide2.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/Templ.cppm -emit-module-interface -o %t/Templ.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -verify -fsyntax-only

//--- Templ.cppm
export module Templ;
export template <typename T>
class Templ {
public:
Templ(T a) {}
};

template<typename T>
Templ(T t) -> Templ<T>;

//--- Use.cpp
// expected-no-diagnostics
import Templ;
void func() {
Templ t(5);
}

26 changes: 26 additions & 0 deletions clang/test/Modules/deduction-guide3.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/Templ.cppm -emit-module-interface -o %t/Templ.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -verify -fsyntax-only

//--- Templ.cppm
export module Templ;
template <typename T>
class Templ {
public:
Templ(T a) {}
};

template<typename T>
Templ(T t) -> Templ<T>;

//--- Use.cpp
import Templ;
void func() {
Templ t(5); // expected-error {{declaration of 'Templ' must be imported from module 'Templ' before it is required}}
// expected-error@-1 {{unknown type name 'Templ'}}
// expected-note@Templ.cppm:3 {{declaration here is not visible}}
}

0 comments on commit 4983fdf

Please sign in to comment.