Skip to content

[SR-15822] Existence of modulemap disables declarations in simple C headers in some cases. #58096

Open
@YOCKOW

Description

@YOCKOW
Previous ID SR-15822
Radar rdar://problem/89641504
Original Reporter @YOCKOW
Type Bug
Environment

Swift 5.5.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, ClangImporter
Assignee None
Priority Medium

md5: 0b3495dbb0f5d0fc852529624d92ab13

relates to:

  • SR-15586 import Foundation hides declarations (derived from C) in other modules on Linux.

Issue Description:

Introduction

This is just a reduced case of SR-15586, however, it indicates that this issue occurs on any platforms; of course, even on macOS.

How to reproduce

Files

<current directory>
+-  library.h
+-  library.swift
+-  MyClangModule
|    +-  module.modulemap
+- user.swift

Contents of files

library.h

// library.h
#ifndef LIBRARY_H__________
#define LIBRARY_H__________
typedef struct _MyCStruct {} MyCStruct;
#endif

library.swift

// library.swift
public typealias SwiftyName = MyCStruct
extension SwiftyName {
  public var foo: String { return "foo" }
  public var foobar: String { return foo + "bar" }
}

MyClangModule/module.modulemap

// MyClangModule/module.modulemap
module MyClangModule {
  header "../library.h"
  export *
}

user.swift

// user.swift
// Unnecessary to `import MyClangModule` to induce the issue
import MySwiftLibrary
let instance = SwiftyName()
print(instance.foo)
print(instance.foobar)

Steps

Compile library

swiftc library.swift \
  -import-objc-header library.h \
  -emit-module -emit-library -module-name MySwiftLibrary

Compile executable

swiftc user.swift -o ./user -I. -L. -lMySwiftLibrary

Result

user.swift:5:16: error: value of type 'SwiftyName' (aka '_MyCStruct') has no member 'foo'
print(instance.foo)
      ~~~~~~~~ ^~~
user.swift:6:16: error: value of type 'SwiftyName' (aka '_MyCStruct') has no member 'foobar'
print(instance.foobar)
      ~~~~~~~~ ^~~~~~

Discussion

The reason why this issue occurs is the same with SR-15586.
As described in the comment in "user.swift", `import MyClangModule` is not necessary to induce this error.
Only the existence of "MyClangModule" directory in one of the search paths causes the error.
Removing "MyClangModule" directory makes it possible to compile "user.swift".

ClangImporter has one unique instance of clang::Parser, clang::Preprocessor, and so on, but it has separate tables for clang::Module s and for declarations derived from simple C headers.
Such implementation brings about this kind of problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions