Skip to content

Commit 3ce8244

Browse files
committed
improve error when using noncopyable type with subscript
Subscripts today don't support any form of ownership specifier for its parameters Since noncopyable types require such a specifier, it's not helpful to emit an error suggesting to add the specifier. rdar://109233314
1 parent 7260c3b commit 3ce8244

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7143,6 +7143,8 @@ ERROR(moveonly_cannot_conform_to_type, none,
71437143
(DescriptiveDeclKind, DeclName, Type))
71447144
ERROR(moveonly_parameter_missing_ownership, none,
71457145
"noncopyable parameter must specify its ownership", ())
7146+
ERROR(moveonly_parameter_subscript_unsupported, none,
7147+
"subscripts cannot have noncopyable parameters yet", ())
71467148
NOTE(moveonly_parameter_ownership_suggestion, none,
71477149
"add '%0' %1", (StringRef, StringRef))
71487150
ERROR(ownership_specifier_copyable,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,31 +2307,39 @@ bool TypeResolver::diagnoseMoveOnlyMissingOwnership(
23072307
if (options.contains(TypeResolutionFlags::HasOwnership))
23082308
return false;
23092309

2310-
// Do not run this on SIL files since there is currently a bug where we are
2311-
// trying to parse it in SILBoxes.
2312-
//
2313-
// To track what we want to do long term: rdar://105635373.
2310+
// Don't diagnose in SIL; ownership is already required there.
23142311
if (options.contains(TypeResolutionFlags::SILType))
23152312
return false;
23162313

2317-
diagnose(repr->getLoc(),
2318-
diag::moveonly_parameter_missing_ownership);
2314+
//////////////////
2315+
// At this point, we know we have a noncopyable parameter that is missing an
2316+
// ownership specifier, so we need to emit an error
2317+
2318+
// We don't yet support any ownership specifiers for parameters of subscript
2319+
// decls, give a tailored error message saying you simply can't use a
2320+
// noncopyable type here.
2321+
if (options.hasBase(TypeResolverContext::SubscriptDecl)) {
2322+
diagnose(repr->getLoc(), diag::moveonly_parameter_subscript_unsupported);
2323+
} else {
2324+
// general error diagnostic
2325+
diagnose(repr->getLoc(),
2326+
diag::moveonly_parameter_missing_ownership);
23192327

2320-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2321-
"borrowing", "for an immutable reference")
2322-
.fixItInsert(repr->getStartLoc(), "borrowing ");
2328+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2329+
"borrowing", "for an immutable reference")
2330+
.fixItInsert(repr->getStartLoc(), "borrowing ");
23232331

2324-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2325-
"inout", "for a mutable reference")
2326-
.fixItInsert(repr->getStartLoc(), "inout ");
2332+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2333+
"inout", "for a mutable reference")
2334+
.fixItInsert(repr->getStartLoc(), "inout ");
23272335

2328-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2329-
"consuming", "to take the value from the caller")
2330-
.fixItInsert(repr->getStartLoc(), "consuming ");
2336+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2337+
"consuming", "to take the value from the caller")
2338+
.fixItInsert(repr->getStartLoc(), "consuming ");
2339+
}
23312340

23322341
// to avoid duplicate diagnostics
23332342
repr->setInvalid();
2334-
23352343
return true;
23362344
}
23372345

test/Sema/moveonly_require_ownership_specifier.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,12 @@ func takeInstantiated(_ x: NoncopyableWrapper<Int>) {}
8484
// expected-note@-2 {{add 'borrowing' for an immutable reference}}{{28-28=borrowing }}
8585
// expected-note@-3 {{add 'inout' for a mutable reference}}{{28-28=inout }}
8686
// expected-note@-4 {{add 'consuming' to take the value from the caller}}{{28-28=consuming }}
87+
88+
struct O: ~Copyable {}
89+
90+
public struct M: ~Copyable {
91+
subscript(_ i: O) -> Int { // expected-error {{subscripts cannot have noncopyable parameters}}
92+
get { fatalError() }
93+
set { }
94+
}
95+
}

0 commit comments

Comments
 (0)