Files
swift-mirror/test/ImportResolution/import-std-fail.swift
Henrik G. Olsson 8d4e81c2fa [ImportResolution] Only disallow explicit std imports (allow implicit)
When expanding a Swift macro in a clang module where the original clang
module imported a submodule in a C++ standard library module other than
`std`, e.g. a submodule to `std_core`, this would result in an error.
This is because `std_core.math.abs` would be imported as
`CxxStdlib.math.abs`, which would later be translated back as
`std.math.abs` which doesn’t exist.

This changes the mapping to only map `std` to `CxxStdlib`. To prevent
errors when importing modules starting with `std_`, this error is moved
from the late-stage module import to the earlier processing of
`ImportDecl`s. This results in these module names still being forbidden
in explicit imports (i.e. naming them in source code), while still being
allowed in implicit imports inherited from clang modules.

This also fixes a fix-it bug where only the first 3 characters would be
selected for replacing with `CxxStdlib` when importing `std_core`.

This also fixes a diagnostic bug where aliased modules would refer to
the module name in the source code rather than the real module name, and
adds a note clarifying the situation.

rdar://161795429
rdar://161795673
rdar://161795793

fix non-fatal import error
2025-10-13 18:51:03 -07:00

43 lines
2.4 KiB
Swift

// 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'}}