Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YOCKOW opened this issue Feb 4, 2022 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@YOCKOW
Copy link
Collaborator

YOCKOW commented Feb 4, 2022

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.

@weissi
Copy link
Member

weissi commented Feb 23, 2022

@swift-ci create

@YOCKOW
Copy link
Collaborator Author

YOCKOW commented Feb 24, 2022

This is definitely the reduced case of SR-15586, but the way why this occurs is slightly more complicated.
MyCStruct's "getClangDecl()" returns:

  • "RecordDecl <library.h> struct definition" where no MyClangModule exists.

  • "RecordDecl <library.h> imported in MyClangModule struct definition" where MyClangModule exists.

This difference seems to induce the failure of IterableDeclContext::loadAllMembers. That is, the compiler cannot find the properties of SwiftyName.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants