Skip to content

[clang++] Implicit template instantiations in modules cause errors in other modules #125521

@BernardoGomesNegri

Description

@BernardoGomesNegri

If module A instantiates a template Class<T>, any module that imports module A and also tries to instantiate Class<T> (with the same T) will get an error:

/home/modBug/reproducer/src/hello.h:8:38: error: declaration 'get' attached to named module 'Mod2' can't be attached to other modules
    8 |     template <typename V> friend int get() noexcept;
      |                                      ^
/home/modBug/reproducer/src/hello.h:3:27: note: also found
    3 | template <typename V> int get() noexcept {return 0;};
      |                           ^

See this reproducer or https://godbolt.org/z/d7YW7TM4a where instantiating List<int> in Mod2 causes any module that also imports Mod2 to fail to instantiate List<int>.
I think this is a bug because the global module fragment is not supposed to be exported.
Reproducer:

hello.h:

template <typename V> int get() noexcept {return 0;};

template <typename T>
class List
{
    template <typename V> friend int get() noexcept;
};

mod.cpp:

module;
#include "hello.h"
export module Mod;
import Mod2;
export const char *modFn() {
    List<int> a;
    List<double> b;
    return modFn2();
}

mod2.cpp:

module;
#include "hello.h"
export module Mod2;
export const char *modFn2() {
    List<int> a;
    return "hello";
}

main.cpp:

#include <iostream>

import Mod;

int main(int argc, char** argv) {
    std::cout << modFn() << '\n';
}

Metadata

Metadata

Assignees

Labels

clang:modulesC++20 modules and Clang Header Modules

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions