Skip to content

Commit ce7de9b

Browse files
authored
Merge pull request #17020 from slavapestov/enum-subscript-type-resolution
Fix regression with inout in enum and subscript signatures
2 parents 854e816 + 7f9d364 commit ce7de9b

File tree

12 files changed

+41
-28
lines changed

12 files changed

+41
-28
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ NOTE(profile_read_error,none,
8787
ERROR(generic_signature_not_minimal,none,
8888
"generic requirement '%0' is redundant in %1", (StringRef, StringRef))
8989

90+
ERROR(attr_only_on_parameters, none,
91+
"'%0' may only be used on parameters", (StringRef))
92+
9093
#ifndef DIAG_NO_UNDEF
9194
# if defined(DIAG)
9295
# undef DIAG

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,6 @@ ERROR(attr_interpolated_string,none,
13091309

13101310
ERROR(attr_only_at_non_local_scope, none,
13111311
"attribute '%0' can only be used in a non-local scope", (StringRef))
1312-
ERROR(attr_only_on_parameters_parse, none,
1313-
"'%0' may only be used on parameters", (StringRef))
13141312

13151313
// Access control
13161314
ERROR(attr_access_expected_set,none,

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,12 +2298,10 @@ ERROR(property_behavior_conformance_broken,none,
22982298

22992299
ERROR(attr_only_one_decl_kind,none,
23002300
"%0 may only be used on '%1' declarations", (DeclAttribute,StringRef))
2301-
ERROR(attr_only_on_parameters,none,
2302-
"%0 may only be used on parameters", (StringRef))
23032301
ERROR(attr_not_on_variadic_parameters,none,
2304-
"%0 must not be used on variadic parameters", (StringRef))
2302+
"'%0' must not be used on variadic parameters", (StringRef))
23052303
ERROR(attr_not_on_subscript_parameters,none,
2306-
"%0 must not be used on subscript parameters", (StringRef))
2304+
"'%0' must not be used on subscript parameters", (StringRef))
23072305

23082306
ERROR(override_final,none,
23092307
"%0 overrides a 'final' %1", (DescriptiveDeclKind, DescriptiveDeclKind))

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4366,8 +4366,9 @@ ParamDecl::ParamDecl(Specifier specifier,
43664366
/*IsCaptureList*/false, parameterNameLoc, parameterName, ty, dc),
43674367
ArgumentName(argumentName), ArgumentNameLoc(argumentNameLoc),
43684368
SpecifierLoc(specifierLoc) {
4369-
assert(specifier != Specifier::Var &&
4370-
"'var' cannot appear on parameters; you meant 'inout'");
4369+
4370+
assert(specifier != Specifier::Var &&
4371+
"'var' cannot appear on parameters; you meant 'inout'");
43714372
Bits.ParamDecl.IsTypeLocImplicit = false;
43724373
Bits.ParamDecl.defaultArgumentKind =
43734374
static_cast<unsigned>(DefaultArgumentKind::None);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5617,7 +5617,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
56175617
// argument label
56185618
auto *paramDecl =
56195619
new (Impl.SwiftContext) ParamDecl(
5620-
VarDecl::Specifier::Let, SourceLoc(), SourceLoc(), argNames.front(),
5620+
VarDecl::Specifier::Default, SourceLoc(), SourceLoc(), argNames.front(),
56215621
SourceLoc(), argNames.front(), Impl.SwiftContext.TheEmptyTupleType,
56225622
dc);
56235623
paramDecl->setInterfaceType(Impl.SwiftContext.TheEmptyTupleType);

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ mapParsedParameters(Parser &parser,
425425
Identifier argName, SourceLoc argNameLoc,
426426
Identifier paramName, SourceLoc paramNameLoc)
427427
-> ParamDecl * {
428-
auto param = new (ctx) ParamDecl(paramInfo.SpecifierKind,
428+
auto param = new (ctx) ParamDecl(VarDecl::Specifier::Default,
429429
paramInfo.SpecifierLoc,
430430
argNameLoc, argName,
431431
paramNameLoc, paramName, Type(),

lib/Parse/ParseType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
181181
Tok.getRawText().equals("__owned")))) {
182182
// Type specifier should already be parsed before here. This only happens
183183
// for construct like 'P1 & inout P2'.
184-
diagnose(Tok.getLoc(), diag::attr_only_on_parameters_parse, Tok.getText());
184+
diagnose(Tok.getLoc(), diag::attr_only_on_parameters, Tok.getRawText());
185185
consumeToken();
186186
}
187187

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,11 +2193,7 @@ Type TypeResolver::resolveASTFunctionType(FunctionTypeRepr *repr,
21932193
options -= TypeResolutionFlags::ImmediateFunctionInput;
21942194
options -= TypeResolutionFlags::FunctionInput;
21952195
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
2196-
// FIXME: Until we remove the IUO type from the type system, we
2197-
// need to continue to parse IUOs in SIL tests so that we can
2198-
// match the types we generate from the importer.
2199-
if (!options.contains(TypeResolutionFlags::SILMode))
2200-
options -= TypeResolutionFlags::AllowIUO;
2196+
options -= TypeResolutionFlags::AllowIUO;
22012197

22022198
Type inputTy = resolveType(repr->getArgsTypeRepr(),
22032199
options | TypeResolutionFlags::ImmediateFunctionInput);
@@ -2712,13 +2708,13 @@ Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
27122708
StringRef name;
27132709
switch (repr->getKind()) {
27142710
case TypeReprKind::InOut:
2715-
name = "'inout'";
2711+
name = "inout";
27162712
break;
27172713
case TypeReprKind::Shared:
2718-
name = "'__shared'";
2714+
name = "__shared";
27192715
break;
27202716
case TypeReprKind::Owned:
2721-
name = "'__owned'";
2717+
name = "__owned";
27222718
break;
27232719
default:
27242720
llvm_unreachable("unknown SpecifierTypeRepr kind");
@@ -2863,11 +2859,7 @@ Type TypeResolver::resolveTupleType(TupleTypeRepr *repr,
28632859
auto elementOptions = options;
28642860
if (repr->isParenType()) {
28652861
// We also want to disallow IUO within even a paren.
2866-
// FIXME: Until we remove the IUO type from the type system, we
2867-
// need to continue to parse IUOs in SIL tests so that we can
2868-
// match the types we generate from the importer.
2869-
if (!options.contains(TypeResolutionFlags::SILMode))
2870-
elementOptions -= TypeResolutionFlags::AllowIUO;
2862+
elementOptions -= TypeResolutionFlags::AllowIUO;
28712863

28722864
// If we have a single ParenType, don't clear the context bits; we
28732865
// still want to parse the type contained therein as if it were in
@@ -2876,7 +2868,7 @@ Type TypeResolver::resolveTupleType(TupleTypeRepr *repr,
28762868
// `FunctionInput` so that e.g. ((foo: Int)) -> Int is considered a
28772869
// tuple argument rather than a labeled Int argument.
28782870
if (isImmediateFunctionInput) {
2879-
elementOptions -= TypeResolutionFlags::ImmediateFunctionInput;
2871+
elementOptions = withoutContext(elementOptions, true);
28802872
elementOptions |= TypeResolutionFlags::FunctionInput;
28812873
}
28822874
} else {

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ withoutContext(TypeResolutionOptions options, bool preserveSIL = false) {
581581
options -= TypeResolutionFlags::FunctionInput;
582582
options -= TypeResolutionFlags::VariadicFunctionInput;
583583
options -= TypeResolutionFlags::EnumCase;
584+
options -= TypeResolutionFlags::SubscriptParameters;
584585
options -= TypeResolutionFlags::ImmediateOptionalTypeArgument;
585586
options -= TypeResolutionFlags::AllowIUO;
586587
if (!preserveSIL) options -= TypeResolutionFlags::SILType;

test/attr/attr_autoclosure.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-typecheck-verify-swift
22

33
// Simple case.
4-
var fn : @autoclosure () -> Int = 4 // expected-error {{@autoclosure may only be used on parameters}} expected-error {{cannot convert value of type 'Int' to specified type '() -> Int'}}
4+
var fn : @autoclosure () -> Int = 4 // expected-error {{'@autoclosure' may only be used on parameters}} expected-error {{cannot convert value of type 'Int' to specified type '() -> Int'}}
55

66
@autoclosure func func1() {} // expected-error {{attribute can only be applied to types, not declarations}}
77

@@ -15,7 +15,7 @@ func func4(fp : @autoclosure () -> Int) {func4(fp: 0)}
1515
func func6(_: @autoclosure () -> Int) {func6(0)}
1616

1717
// autoclosure + inout doesn't make sense.
18-
func func8(_ x: inout @autoclosure () -> Bool) -> Bool { // expected-error {{@autoclosure may only be used on parameters}}
18+
func func8(_ x: inout @autoclosure () -> Bool) -> Bool { // expected-error {{'@autoclosure' may only be used on parameters}}
1919
}
2020

2121
func func9(_ x: @autoclosure (Int) -> Bool) {} // expected-error {{argument type of @autoclosure parameter must be '()'}}
@@ -157,7 +157,7 @@ func callAutoclosureWithNoEscape_3(_ fn: @autoclosure () -> Int) {
157157
takesAutoclosure(fn()) // ok
158158
}
159159

160-
// expected-error @+1 {{@autoclosure must not be used on variadic parameters}}
160+
// expected-error @+1 {{'@autoclosure' must not be used on variadic parameters}}
161161
func variadicAutoclosure(_ fn: @autoclosure () -> ()...) {
162162
for _ in fn {}
163163
}

test/decl/enum/enumtest.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,7 @@ func useSynthesizedMember() {
322322
enum Lens<T> {
323323
case foo(inout T) // expected-error {{'inout' may only be used on parameters}}
324324
case bar(inout T, Int) // expected-error {{'inout' may only be used on parameters}}
325+
326+
case baz((inout T) -> ()) // ok
327+
case quux((inout T, inout T) -> ()) // ok
325328
}

test/decl/subscript/subscripting.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,20 @@ struct SR2575 {
369369

370370
SR2575().subscript()
371371
// expected-error@-1 {{value of type 'SR2575' has no property or method named 'subscript'; did you mean to use the subscript operator?}} {{9-10=}} {{10-19=}} {{19-20=[}} {{20-21=]}}
372+
373+
// SR-7890
374+
375+
struct InOutSubscripts {
376+
subscript(x1: inout Int) -> Int { return 0 }
377+
// expected-error@-1 {{'inout' must not be used on subscript parameters}}
378+
379+
subscript(x2: inout Int, y2: inout Int) -> Int { return 0 }
380+
// expected-error@-1 2{{'inout' must not be used on subscript parameters}}
381+
382+
subscript(x3: (inout Int) -> ()) -> Int { return 0 } // ok
383+
subscript(x4: (inout Int, inout Int) -> ()) -> Int { return 0 } // ok
384+
385+
subscript(inout x5: Int) -> Int { return 0 }
386+
// expected-error@-1 {{'inout' before a parameter name is not allowed, place it before the parameter type instead}}
387+
// expected-error@-2 {{'inout' must not be used on subscript parameters}}
388+
}

0 commit comments

Comments
 (0)