Skip to content

Commit 8346bc6

Browse files
committed
Allow :: as an alias for . in scoped imports
1 parent 8422e4a commit 8346bc6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ NOTE(extra_whitespace_module_selector,none,
101101
"remove extraneous whitespace after '::'", ())
102102
ERROR(module_selector_submodule_not_allowed,none,
103103
"module selector cannot specify a submodule", ())
104+
NOTE(replace_module_selector_with_member_lookup,none,
105+
"replace '::' with '.'", ())
104106

105107
//------------------------------------------------------------------------------
106108
// MARK: Lexer diagnostics

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6681,7 +6681,13 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
66816681
.fixItRemove(Tok.getLoc());
66826682
return nullptr;
66836683
}
6684-
HasNext = consumeIf(tok::period);
6684+
if (Tok.is(tok::colon_colon)
6685+
&& (Kind == ImportKind::Module || importPath.size() > 1)) {
6686+
diagnose(Tok, diag::module_selector_submodule_not_allowed);
6687+
diagnose(Tok, diag::replace_module_selector_with_member_lookup)
6688+
.fixItReplace(Tok.getLoc(), ".");
6689+
}
6690+
HasNext = consumeIf(tok::period) || consumeIf(tok::colon_colon);
66856691
} while (HasNext);
66866692

66876693
if (Tok.is(tok::code_complete)) {

test/Parse/module_selector.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55

66
// REQUIRES: swift_feature_ModuleSelector
77

8+
// ModuleSelectorImports
9+
import struct ModuleSelectorTestingKit::A
10+
11+
import struct _::A
12+
// expected-error@-1 {{'_' cannot be used as an identifier here}}
13+
// expected-note@-2 {{if this name is unavoidable, use backticks to escape it}} {{15-16=`_`}}
14+
15+
import struct ModuleSelectorTestingKit::Submodule::A
16+
// expected-error@-1 {{module selector cannot specify a submodule}}
17+
// expected-note@-2 {{replace '::' with '.'}} {{50-52=.}}
18+
19+
import struct ModuleSelectorTestingKit.Submodule::A
20+
// expected-error@-1 {{module selector cannot specify a submodule}}
21+
// expected-note@-2 {{replace '::' with '.'}} {{49-51=.}}
22+
23+
import ctypes::bits
24+
// expected-error@-1 {{module selector cannot specify a submodule}}
25+
// expected-note@-2 {{replace '::' with '.'}} {{14-16=.}}
26+
27+
828
// ModuleSelectorCorrectCode
929
extension ModuleSelectorTestingKit::A {}
1030

0 commit comments

Comments
 (0)