-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Implicit import fixes #84642
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
Open
hnrklssn
wants to merge
8
commits into
swiftlang:main
Choose a base branch
from
hnrklssn:implicit-import-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implicit import fixes #84642
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da1fa74
[MacrosOnImports] Don't import !swift modules
hnrklssn fca94fb
[ImportResolution] Only disallow explicit `std` imports (allow implicit)
hnrklssn 0b10caf
remove unnecessary c++ interop
hnrklssn a17ad02
fix non-fatal import error
hnrklssn a6c0778
only run transitive-std-core-import.swift on new libc++
hnrklssn 3e25af2
[DiagnosticVerifier] Add -verify-ignore-macro-note
hnrklssn f828474
fix test on Windows
hnrklssn 6ebb5e4
don't run %empty-directory twice
hnrklssn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// RUN: %target-typecheck-verify-swift -module-alias Foo=Bar | ||
|
||
import Foo // expected-error{{no such module 'Bar'}} | ||
// expected-note@-1{{module name 'Foo' is aliased to 'Bar'}} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: split-file %s %t | ||
|
||
// These errors are fatal, so test each one separately | ||
|
||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/a.swift | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/b.swift | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/c.swift | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/d.swift | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/e.swift -verify-additional-prefix aliased- -module-alias FooBar=std_foo_bar | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/e.swift -verify-additional-prefix unaliased- | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/f.swift -verify-additional-prefix aliased- -module-alias X=std_x_h | ||
// RUN: %target-swift-frontend -typecheck -verify -cxx-interoperability-mode=default %t/f.swift -verify-additional-prefix unaliased- | ||
|
||
//--- a.swift | ||
import std // expected-error{{no such module 'std'}} | ||
// expected-note@-1{{did you mean 'CxxStdlib'?}}{{8-11=CxxStdlib}} {{none}} | ||
|
||
//--- b.swift | ||
import std_ // expected-error{{no such module 'std_'}} | ||
// expected-note@-1{{did you mean 'CxxStdlib'?}}{{8-12=CxxStdlib}} {{none}} | ||
|
||
//--- c.swift | ||
import std_error_h // expected-error{{no such module 'std_error_h'}} | ||
// expected-note@-1{{did you mean 'CxxStdlib'?}}{{8-19=CxxStdlib}} {{none}} | ||
|
||
//--- d.swift | ||
import std_core.math.abs // expected-error{{no such module 'std_core.math.abs'}} | ||
// expected-note@-1{{did you mean 'CxxStdlib'?}}{{8-16=CxxStdlib}} {{none}} | ||
|
||
//--- e.swift | ||
import FooBar // expected-aliased-error{{no such module 'std_foo_bar'}} | ||
// expected-aliased-note@-1{{did you mean 'CxxStdlib'?}}{{8-14=CxxStdlib}} {{none}} | ||
// expected-aliased-note@-2{{module name 'FooBar' is aliased to 'std_foo_bar'}} {{none}} | ||
// expected-unaliased-error@-3{{no such module 'FooBar'}} | ||
|
||
//--- f.swift | ||
import X | ||
// expected-aliased-error@-1{{no such module 'std_x_h'}} | ||
// expected-aliased-note@-2{{did you mean 'CxxStdlib'?}}{{8-9=CxxStdlib}} {{none}} | ||
// expected-aliased-note@-3{{module name 'X' is aliased to 'std_x_h'}} {{none}} | ||
// expected-unaliased-error@-4{{no such module 'X'}} |
81 changes: 81 additions & 0 deletions
81
test/Interop/C/swiftify-import/clang-includes-no-swift.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: split-file --leading-lines %s %t | ||
|
||
// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/succeed.swift | ||
// RUN: not %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/fail.swift -dump-source-file-imports 2>&1 | %FileCheck %s | ||
// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -I %t%{fs-sep}Inputs -enable-experimental-feature SafeInteropWrappers %t/fail.swift -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}B1.h -verify-ignore-macro-note | ||
|
||
// Tests that we don't try to import modules that don't work well with Swift | ||
|
||
// CHECK: imports for {{.*}}fail.swift: | ||
// CHECK-NEXT: Swift | ||
// CHECK-NEXT: _StringProcessing | ||
// CHECK-NEXT: _SwiftConcurrencyShims | ||
// CHECK-NEXT: _Concurrency | ||
// CHECK-NEXT: B1 | ||
// CHECK-NEXT: A1 | ||
|
||
// CHECK-NEXT: imports for A1.foo: | ||
|
||
// CHECK-NEXT: imports for @__swiftmacro{{.*}}foo{{.*}}_SwiftifyImport{{.*}}.swift: | ||
// CHECK-NEXT: Swift | ||
// CHECK-NEXT: B1 | ||
// CHECK-NEXT: _StringProcessing | ||
// CHECK-NEXT: _SwiftConcurrencyShims | ||
// CHECK-NEXT: _Concurrency | ||
|
||
//--- Inputs/module.modulemap | ||
module A1 { | ||
explicit module B1 { | ||
header "B1.h" | ||
explicit module C1 { | ||
header "C1.h" | ||
requires !swift | ||
} | ||
} | ||
} | ||
|
||
//--- Inputs/B1.h | ||
#pragma once | ||
|
||
#include "C1.h" | ||
#define __sized_by(s) __attribute__((__sized_by__(s))) | ||
|
||
// We can use bar without C1 causing errors | ||
void bar(void * _Nonnull __sized_by(size), int size); | ||
// foo causes an error when we try to refer to c1_t from the '!swift' module C1 | ||
c1_t foo(void * _Nonnull __sized_by(size), int size); | ||
/* | ||
expected-note@-2{{'foo' declared here}} | ||
expected-expansion@-3:52{{ | ||
expected-error@2:110{{cannot find type 'c1_t' in scope}} | ||
}} | ||
*/ | ||
|
||
//--- Inputs/C1.h | ||
#pragma once | ||
|
||
typedef int c1_t; | ||
|
||
//--- fail.swift | ||
import A1.B1 | ||
|
||
public func callUnsafe(_ p: UnsafeMutableRawPointer) { | ||
let _ = foo(p, 13) | ||
} | ||
public func callSafe(_ p: UnsafeMutableRawBufferPointer) { | ||
let _ = foo(p) // expected-error{{cannot convert value of type 'UnsafeMutableRawBufferPointer' to expected argument type 'UnsafeMutableRawPointer'}} | ||
// expected-error@-1{{missing argument}} | ||
} | ||
|
||
//--- succeed.swift | ||
import A1.B1 | ||
|
||
public func callUnsafe(_ p: UnsafeMutableRawPointer) { | ||
bar(p, 13) | ||
} | ||
public func callSafe(_ p: UnsafeMutableRawBufferPointer) { | ||
bar(p) | ||
} |
38 changes: 38 additions & 0 deletions
38
test/Interop/Cxx/swiftify-import/transitive-std-core-import.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
// REQUIRES: OS=macosx | ||
|
||
// Don't run this test with libc++ versions 17-19, when the top-level std module was split into multiple top-level modules. | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-clangxx %S/../stdlib/Inputs/check-libcxx-version.cpp -o %t/check-libcxx-version | ||
// RUN: %target-codesign %t/check-libcxx-version | ||
|
||
// RUN: %target-run %t/check-libcxx-version || split-file %s %t | ||
// RUN: %target-run %t/check-libcxx-version || %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -cxx-interoperability-mode=default -Xcc -std=c++20 | ||
|
||
// Test that implicit imports can refer to std_core etc., even though Swift source code may not. | ||
|
||
//--- Inputs/module.modulemap | ||
module TestClang { | ||
header "test.h" | ||
requires cplusplus | ||
export std_core.cstddef.size_t | ||
export span | ||
} | ||
|
||
//--- Inputs/test.h | ||
|
||
#include <__cstddef/size_t.h> | ||
#include <span> | ||
#include <lifetimebound.h> | ||
|
||
using FloatSpan = std::span<const float>; | ||
|
||
size_t foo(FloatSpan x __noescape); | ||
|
||
//--- test.swift | ||
import TestClang | ||
|
||
func test(x: Span<Float>) { | ||
let _ = foo(x) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this still accept
std_core
, which acts as a top-level module in recent libc++?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is specifically done to exclude top-level libc++ modules other than
std
. The issue is that while you could specify submodules toCxxStdlib
, there was no way to inherit imports to other libc++ submodules. E.g. a clang module importingstd_core.math.abs
would become an import ofCxxStdlib.math.abs
on the Swift side, which would then be translated back tostd.math.abs
(which doesn't exist) when looking up the module.