Skip to content

Commit 240fc5e

Browse files
authored
Merge pull request #3881 from DougGregor/enable-se-0111
SE-0111: Enable removal of argument labels in function types by default
2 parents b7023d6 + 5b67fe4 commit 240fc5e

File tree

92 files changed

+426
-354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+426
-354
lines changed

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@ Note: This is in reverse chronological order, so newer entries are added to the
33
Swift 3.0
44
---------
55

6+
* [SE-0111](https://github.com/apple/swift-evolution/blob/master/proposals/0111-remove-arg-label-type-significance.md):
7+
8+
Argument labels have been removed from Swift function types. Instead, they are
9+
part of the name of a function, subscript, or initializer. Calls to a function
10+
or initializer, or uses of a subscript, still require argument labels, as they
11+
always have:
12+
13+
```swift
14+
func doSomething(x: Int, y: Int) { }
15+
doSomething(x: 0, y: 0) // argument labels are required
16+
```
17+
18+
However, unapplied references to functions or initializers no longer carry
19+
argument labels. For example:
20+
21+
```swift
22+
let f = doSomething(x:y:) // inferred type is now (Int, Int) -> Void
23+
```
24+
25+
Additionally, explicitly-written function types can no longer carry argument
26+
labels, although one can still provide parameter name for documentation
27+
purposes using the '_' in the argument label position:
28+
29+
```swift
30+
typealias CompletionHandler =
31+
(token: Token, error: Error?) -> Void // error: function types cannot have argument labels
32+
33+
typealias CompletionHandler =
34+
(_ token: Token, _ error: Error?) -> Void // error: okay: names are for documentation purposes
35+
```
36+
637
* [SE-0025](https://github.com/apple/swift-evolution/blob/master/proposals/0025-scoped-access-level.md): A declaration marked as `private` can now only be accessed within the lexical scope it is declared in (essentially the enclosing curly braces `{}`). A `private` declaration at the top level of a file can be accessed anywhere in that file, as in Swift 2. The access level formerly known as `private` is now called `fileprivate`.
738

839
* [SE-0131](https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md):

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,16 +3835,14 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
38353835

38363836
// If we're stripping argument labels from types, do it when printing.
38373837
Type inputType = T->getInput();
3838-
if (inputType->getASTContext().LangOpts.SuppressArgumentLabelsInTypes) {
3839-
if (auto tupleTy = dyn_cast<TupleType>(inputType.getPointer())) {
3840-
SmallVector<TupleTypeElt, 4> elements;
3841-
elements.reserve(tupleTy->getNumElements());
3842-
for (const auto &elt : tupleTy->getElements()) {
3843-
elements.push_back(TupleTypeElt(elt.getType(), Identifier(),
3844-
elt.isVararg()));
3845-
}
3846-
inputType = TupleType::get(elements, inputType->getASTContext());
3838+
if (auto tupleTy = dyn_cast<TupleType>(inputType.getPointer())) {
3839+
SmallVector<TupleTypeElt, 4> elements;
3840+
elements.reserve(tupleTy->getNumElements());
3841+
for (const auto &elt : tupleTy->getElements()) {
3842+
elements.push_back(TupleTypeElt(elt.getType(), Identifier(),
3843+
elt.isVararg()));
38473844
}
3845+
inputType = TupleType::get(elements, inputType->getASTContext());
38483846
}
38493847

38503848
bool needsParens =

lib/Parse/ParseType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
681681
SourceLoc secondNameLoc = std::get<3>(currentLabel);
682682

683683
// True tuples have labels.
684-
if (!isFunctionType || !Context.LangOpts.SuppressArgumentLabelsInTypes) {
684+
if (!isFunctionType) {
685685
// If there were two names, complain.
686686
if (firstNameLoc.isValid() && secondNameLoc.isValid()) {
687687
auto diag = diagnose(firstNameLoc, diag::tuple_type_multiple_labels);

lib/SILGen/RValue.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,17 @@ getElementRange(CanTupleType tupleType, unsigned eltIndex) {
559559
RValue RValue::extractElement(unsigned n) && {
560560
assert(isComplete() && "rvalue is not complete");
561561

562-
auto tupleTy = cast<TupleType>(type);
562+
CanTupleType tupleTy = dyn_cast<TupleType>(type);
563+
if (!tupleTy) {
564+
assert(n == 0);
565+
unsigned to = getRValueSize(type);
566+
assert(to == values.size());
567+
RValue element({llvm::makeArrayRef(values).slice(0, to), type});
568+
makeUsed();
569+
return element;
570+
}
571+
572+
563573
auto range = getElementRange(tupleTy, n);
564574
unsigned from = range.first, to = range.second;
565575

@@ -572,8 +582,17 @@ RValue RValue::extractElement(unsigned n) && {
572582
void RValue::extractElements(SmallVectorImpl<RValue> &elements) && {
573583
assert(isComplete() && "rvalue is not complete");
574584

585+
CanTupleType tupleTy = dyn_cast<TupleType>(type);
586+
if (!tupleTy) {
587+
unsigned to = getRValueSize(type);
588+
assert(to == values.size());
589+
elements.push_back({llvm::makeArrayRef(values).slice(0, to), type});
590+
makeUsed();
591+
return;
592+
}
593+
575594
unsigned from = 0;
576-
for (auto eltType : cast<TupleType>(type).getElementTypes()) {
595+
for (auto eltType : tupleTy.getElementTypes()) {
577596
unsigned to = from + getRValueSize(eltType);
578597
elements.push_back({llvm::makeArrayRef(values).slice(from, to - from),
579598
eltType});

lib/SILGen/SILGenApply.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,19 +2967,27 @@ namespace {
29672967
// If we're working with an r-value, just expand it out and emit
29682968
// all the elements individually.
29692969
if (arg.isRValue()) {
2970-
CanTupleType substArgType = cast<TupleType>(arg.getSubstType());
2971-
2972-
// The original type isn't necessarily a tuple.
2973-
assert(origParamType.matchesTuple(substArgType));
2974-
2975-
auto loc = arg.getKnownRValueLocation();
2976-
SmallVector<RValue, 4> elts;
2977-
std::move(arg).asKnownRValue().extractElements(elts);
2978-
for (auto i : indices(substArgType.getElementTypes())) {
2979-
emit({ loc, std::move(elts[i]) },
2980-
origParamType.getTupleElementType(i));
2981-
}
2982-
return;
2970+
if (CanTupleType substArgType =
2971+
dyn_cast<TupleType>(arg.getSubstType())) {
2972+
// The original type isn't necessarily a tuple.
2973+
assert(origParamType.matchesTuple(substArgType));
2974+
2975+
auto loc = arg.getKnownRValueLocation();
2976+
SmallVector<RValue, 4> elts;
2977+
std::move(arg).asKnownRValue().extractElements(elts);
2978+
for (auto i : indices(substArgType.getElementTypes())) {
2979+
emit({ loc, std::move(elts[i]) },
2980+
origParamType.getTupleElementType(i));
2981+
}
2982+
return;
2983+
}
2984+
2985+
auto loc = arg.getKnownRValueLocation();
2986+
SmallVector<RValue, 1> elts;
2987+
std::move(arg).asKnownRValue().extractElements(elts);
2988+
emit({ loc, std::move(elts[0]) },
2989+
origParamType.getTupleElementType(0));
2990+
return;
29832991
}
29842992

29852993
// Otherwise, we're working with an expression.

lib/SILGen/SILGenPoly.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,16 @@ static void emitForceInto(SILGenFunction &SGF, SILLocation loc,
772772
temp.finishInitialization(SGF);
773773
}
774774

775+
/// If the type is a single-element tuple, return the element type.
776+
static CanType getSingleTupleElement(CanType type) {
777+
if (auto tupleType = dyn_cast<TupleType>(type)) {
778+
if (tupleType->getNumElements() == 1)
779+
return tupleType.getElementType(0);
780+
}
781+
782+
return type;
783+
}
784+
775785
namespace {
776786
class TranslateArguments {
777787
SILGenFunction &SGF;
@@ -804,18 +814,15 @@ namespace {
804814
if (inputOrigType.isTuple() &&
805815
inputOrigType.getNumTupleElements() == 1) {
806816
inputOrigType = inputOrigType.getTupleElementType(0);
807-
inputSubstType = cast<TupleType>(inputSubstType).getElementType(0);
817+
inputSubstType = getSingleTupleElement(inputSubstType);
808818
return translate(inputOrigType, inputSubstType,
809819
outputOrigType, outputSubstType);
810820
}
811821

812822
if (outputOrigType.isTuple() &&
813823
outputOrigType.getNumTupleElements() == 1) {
814824
outputOrigType = outputOrigType.getTupleElementType(0);
815-
if (auto outputSubstTuple = dyn_cast<TupleType>(outputSubstType)) {
816-
if (outputSubstTuple->getNumElements() > 0)
817-
outputSubstType = outputSubstTuple.getElementType(0);
818-
}
825+
outputSubstType = getSingleTupleElement(outputSubstType);
819826
return translate(inputOrigType, inputSubstType,
820827
outputOrigType, outputSubstType);
821828
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,6 @@ void ConstraintSystem::recordOpenedTypes(
819819
static unsigned getNumRemovedArgumentLabels(ASTContext &ctx, ValueDecl *decl,
820820
bool isCurriedInstanceReference,
821821
FunctionRefKind functionRefKind) {
822-
// Is this functionality enabled at all?
823-
if (!ctx.LangOpts.SuppressArgumentLabelsInTypes) return 0;
824-
825822
// Only applicable to functions. Nothing else should have argument labels in
826823
// the type.
827824
auto func = dyn_cast<AbstractFunctionDecl>(decl);

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,9 @@ namespace {
778778
assert(ExprStack.back() == expr);
779779
ExprStack.pop_back();
780780

781-
// When we're suppressing argument labels in types, mark the direct callee
782-
// as being a callee.
783-
if (TC.Context.LangOpts.SuppressArgumentLabelsInTypes) {
784-
if (auto call = dyn_cast<CallExpr>(expr))
785-
markDirectCallee(call->getFn());
786-
}
781+
// Mark the direct callee as being a callee.
782+
if (auto call = dyn_cast<CallExpr>(expr))
783+
markDirectCallee(call->getFn());
787784

788785
// Fold sequence expressions.
789786
if (auto seqExpr = dyn_cast<SequenceExpr>(expr)) {

stdlib/private/StdlibUnittest/StringConvertible.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct CustomPrintableValue
3030
public static var timesDebugDescriptionWasCalled = ResettableValue(0)
3131

3232
public static var descriptionImpl =
33-
ResettableValue<(value: Int, identity: Int) -> String>({
33+
ResettableValue<(_ value: Int, _ identity: Int) -> String>({
3434
(value: Int, identity: Int) -> String in
3535
if identity == 0 {
3636
return "(value: \(value)).description"
@@ -40,7 +40,7 @@ public struct CustomPrintableValue
4040
})
4141

4242
public static var debugDescriptionImpl =
43-
ResettableValue<(value: Int, identity: Int) -> String>({
43+
ResettableValue<(_ value: Int, _ identity: Int) -> String>({
4444
(value: Int, identity: Int) -> String in
4545
CustomPrintableValue.timesDescriptionWasCalled.value += 1
4646
if identity == 0 {
@@ -97,15 +97,15 @@ extension CustomPrintableValue : CustomStringConvertible {
9797
public var description: String {
9898
CustomPrintableValue.timesDescriptionWasCalled.value += 1
9999
return CustomPrintableValue.descriptionImpl.value(
100-
value: value, identity: identity)
100+
value, identity)
101101
}
102102
}
103103

104104
extension CustomPrintableValue : CustomDebugStringConvertible {
105105
public var debugDescription: String {
106106
CustomPrintableValue.timesDebugDescriptionWasCalled.value += 1
107107
return CustomPrintableValue.debugDescriptionImpl.value(
108-
value: value, identity: identity)
108+
value, identity)
109109
}
110110
}
111111

stdlib/public/SDK/Dispatch/Data.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
8282
}
8383

8484
public func enumerateBytes(
85-
block: @noescape (buffer: UnsafeBufferPointer<UInt8>, byteIndex: Int, stop: inout Bool) -> Void)
85+
block: @noescape (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Int, _ stop: inout Bool) -> Void)
8686
{
8787
_swift_dispatch_data_apply(__wrapped) { (_, offset: Int, ptr: UnsafeRawPointer, size: Int) in
8888
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: size)
8989
let bp = UnsafeBufferPointer(start: bytePtr, count: size)
9090
var stop = false
91-
block(buffer: bp, byteIndex: offset, stop: &stop)
91+
block(bp, offset, &stop)
9292
return stop ? 0 : 1
9393
}
9494
}

0 commit comments

Comments
 (0)