Skip to content

Commit 827acad

Browse files
committed
Various inout improvements:
- purge @inout from comments in the compiler except for places talking about the SIL argument convention. - change diagnostics to not refer to @inout - Change the astprinter to print InoutType without the @, so it doesn't show up in diagnostics or in closure argument types in code completion. - Implement type parsing support for the new inout syntax (before we just handled patterns). - Switch the last couple of uses in the stdlib (in types) to inout. - Various testcase updates (more to come). Swift SVN r13564
1 parent 79cbda7 commit 827acad

Some content is hidden

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

41 files changed

+112
-97
lines changed

Diff for: docs/LangRef.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ <h4 id="attribute-inout"><tt>inout</tt> Attribute</h4>
15771577
For example, the following code:
15781578

15791579
<pre class=example>
1580-
func foo(x: @inout Int) -> () -> Int {
1580+
func foo(inout x: Int) -> () -> Int {
15811581
func bar() -> Int {
15821582
x += 1
15831583
return x
@@ -2073,7 +2073,7 @@ <h3 id="type-optional">Optional Types</h3>
20732073
To support these intrinsic use cases, the library is required to
20742074
provide four functions with these exact signatures:
20752075
<ul>
2076-
<li><code>func _doesOptionalHaveValue<T>(v : @inout T?) -> Builtin.Int1</code></li>
2076+
<li><code>func _doesOptionalHaveValue<T>(inout v : T?) -> Builtin.Int1</code></li>
20772077
<li><code>func _getOptionalValue<T>(v : T?) -> T</code></li>
20782078
<li><code>func _injectValueIntoOptional<T>(v : T) -> T?</code></li>
20792079
<li><code>func _injectNothingIntoOptional<T>() -> T?</code></li>

Diff for: include/swift/AST/DiagnosticsSema.def

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ ERROR(prefix_not_an_operator,sema_tcd,none,
224224
ERROR(invalid_prefix_input,sema_tcd,none,
225225
"only unary operators can be declared 'prefix'", ())
226226
ERROR(assignment_without_inout,sema_tcd,none,
227-
"assignment operator must have an initial @inout argument", ())
227+
"assignment operator must have an initial inout argument", ())
228228
ERROR(mutating_invalid_global_scope,sema_tcd,none,
229229
"'mutating' is only valid on methods", ())
230230
ERROR(mutating_invalid_classes,sema_tcd,none,
@@ -546,7 +546,7 @@ ERROR(incompatible_assoc,sema_tce,none,
546546
"operator is adjacent to operator of same precedence"
547547
" but incompatible associativity", ())
548548
ERROR(reference_non_inout,sema_tce,none,
549-
"reference to %0 not used to initialize a @inout parameter", (Type))
549+
"reference to %0 not used to initialize a inout parameter", (Type))
550550
NOTE(subscript_decl_here,sema_tca,none,
551551
"subscript operator declared here", ())
552552
ERROR(condition_broken_proto,sema_tce,none,

Diff for: include/swift/AST/Types.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ class alignas(8) TypeBase {
457457
/// Otherwise, returns the type itself.
458458
Type getRValueType();
459459

460-
/// getInOutObjectType - For an @inout type, retrieves the underlying object
460+
/// getInOutObjectType - For an inout type, retrieves the underlying object
461461
/// type. Otherwise, returns the type itself.
462462
Type getInOutObjectType();
463463

464-
/// getLValueOrInOutObjectType - For an @lvalue or @inout type, retrieves the
464+
/// getLValueOrInOutObjectType - For an @lvalue or inout type, retrieves the
465465
/// underlying object type. Otherwise, returns the type itself.
466466
Type getLValueOrInOutObjectType();
467467

@@ -2507,9 +2507,8 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(ProtocolCompositionType, Type)
25072507
/// - it is a tuple and at least one of its element types
25082508
/// carries an l-value.
25092509
///
2510-
/// The type of a function argument may carry an l-value. This
2511-
/// is done by annotating the bound variable with the @inout
2512-
/// attribute.
2510+
/// The type of a function argument may carry an l-value. This is done by
2511+
/// annotating the bound variable with InOutType.
25132512
///
25142513
/// The type of a return value, local variable, or field may not
25152514
/// carry an l-value.
@@ -2542,7 +2541,7 @@ BEGIN_CAN_TYPE_WRAPPER(LValueType, Type)
25422541
}
25432542
END_CAN_TYPE_WRAPPER(LValueType, Type)
25442543

2545-
/// InOutType - An @inout qualified type is an argument to a function passed
2544+
/// InOutType - An inout qualified type is an argument to a function passed
25462545
/// with an explicit "Address of" operator. It is read in and then written back
25472546
/// to after the callee function is done. This also models the receiver of
25482547
/// @mutable methods on value types.
@@ -3103,7 +3102,7 @@ inline bool TypeBase::isBuiltinIntegerType(unsigned n) {
31033102
return false;
31043103
}
31053104

3106-
/// getInOutObjectType - For an @inout type, retrieves the underlying object
3105+
/// getInOutObjectType - For an inout type, retrieves the underlying object
31073106
/// type. Otherwise, returns the type itself.
31083107
inline Type TypeBase::getInOutObjectType() {
31093108
if (auto iot = getAs<InOutType>())
@@ -3117,7 +3116,7 @@ inline Type TypeBase::getRValueType() {
31173116
return this;
31183117
}
31193118

3120-
/// getLValueOrInOutObjectType - For an @lvalue or @inout type, retrieves the
3119+
/// getLValueOrInOutObjectType - For an @lvalue or inout type, retrieves the
31213120
/// underlying object type. Otherwise, returns the type itself.
31223121
inline Type TypeBase::getLValueOrInOutObjectType() {
31233122
if (auto iot = getAs<InOutType>())

Diff for: lib/AST/ASTContext.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ FuncDecl *ASTContext::getDoesOptionalHaveValueDecl(LazyResolver *resolver,
539539
if (!decl || !isGenericIntrinsic(decl, input, output, param))
540540
return nullptr;
541541

542-
// Input must be @inout Optional<T>.
542+
// Input must be inout Optional<T>.
543543
auto inputInOut = dyn_cast<InOutType>(input);
544544
if (!inputInOut || !isOptionalType(*this, optionalKind,
545545
inputInOut.getObjectType(), param))
@@ -1784,7 +1784,7 @@ LValueType *LValueType::get(Type objectTy) {
17841784
assert(!objectTy->is<ErrorType>() &&
17851785
"can not have ErrorType wrapped inside LValueType");
17861786
assert(!objectTy->is<LValueType>() && !objectTy->is<InOutType>() &&
1787-
"can not have @inout or @lvalue wrapped inside an @lvalue");
1787+
"can not have 'inout' or @lvalue wrapped inside an @lvalue");
17881788

17891789
auto properties = objectTy->getRecursiveProperties()
17901790
+ RecursiveTypeProperties::IsNotMaterializable;
@@ -1804,7 +1804,7 @@ InOutType *InOutType::get(Type objectTy) {
18041804
assert(!objectTy->is<ErrorType>() &&
18051805
"can not have ErrorType wrapped inside InOutType");
18061806
assert(!objectTy->is<LValueType>() && !objectTy->is<InOutType>() &&
1807-
"can not have @inout or @lvalue wrapped inside an @inout");
1807+
"can not have 'inout' or @lvalue wrapped inside an 'inout'");
18081808

18091809
auto properties = objectTy->getRecursiveProperties()
18101810
+ RecursiveTypeProperties::IsNotMaterializable;

Diff for: lib/AST/ASTPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
15681568
}
15691569

15701570
void visitInOutType(InOutType *T) {
1571-
Printer << "@inout ";
1571+
Printer << "inout ";
15721572
visit(T->getObjectType());
15731573
}
15741574

Diff for: lib/AST/Decl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1316,14 +1316,14 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
13161316
} else if (isa<ConstructorDecl>(theMethod)) {
13171317
if (isInitializingCtor) {
13181318
// initializing constructors of value types always have an implicitly
1319-
// @inout self.
1319+
// inout self.
13201320
isMutating = true;
13211321
} else {
13221322
// allocating constructors have metatype 'self'.
13231323
isStatic = true;
13241324
}
13251325
} else if (isa<DestructorDecl>(theMethod)) {
1326-
// destructors of value types always have an implicitly @inout self.
1326+
// destructors of value types always have an implicitly inout self.
13271327
isMutating = true;
13281328
}
13291329

@@ -1362,12 +1362,12 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
13621362
if (containerTy->hasReferenceSemantics())
13631363
return selfTy;
13641364

1365-
// Mutating methods are always passed @inout so we can receive the side
1365+
// Mutating methods are always passed inout so we can receive the side
13661366
// effect.
13671367
//
13681368
// With non-mutating methods on value types, we generally pass the value
13691369
// directly in at +1. The exception is for protocol methods, which we pass
1370-
// @inout at +0. We handle the abstraction difference in the witness thunk for
1370+
// inout at +0. We handle the abstraction difference in the witness thunk for
13711371
// the received method, where we know the concrete receiver type. We do this
13721372
// by having existential_member_ref and archetype_member_ref take the 'self'
13731373
// base object as an rvalue for @!mutating protocol members, even though that

Diff for: lib/AST/NameLookup.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ bool DeclContext::lookupQualified(Type type,
774774
return false;
775775

776776

777-
// Look through lvalue and @inout types.
777+
// Look through lvalue and inout types.
778778
type = type->getLValueOrInOutObjectType();
779779

780780
// Look through metatypes.

Diff for: lib/AST/Type.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ Type TypeBase::getRValueInstanceType() {
504504
if (auto metaTy = type->getAs<MetatypeType>())
505505
return metaTy->getInstanceType();
506506

507-
// For @mutable value type methods, we need to dig through @inout types.
507+
// For @mutable value type methods, we need to dig through inout types.
508508
return type->getInOutObjectType();
509509
}
510510

Diff for: lib/AST/Verifier.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,12 @@ struct ASTNodeBase {};
761761
abort();
762762
}
763763

764-
// The only time the base is allowed to be @inout is if we are accessing
764+
// The only time the base is allowed to be inout is if we are accessing
765765
// a computed property.
766766
if (E->getBase()->getType()->is<InOutType>()) {
767767
VarDecl *VD = dyn_cast<VarDecl>(E->getMember().getDecl());
768768
if (!VD || !VD->hasAccessorFunctions()) {
769-
Out << "member_ref_expr on value of @inout type\n";
769+
Out << "member_ref_expr on value of inout type\n";
770770
E->dump(Out);
771771
abort();
772772
}
@@ -1544,7 +1544,7 @@ struct ASTNodeBase {};
15441544
if (InOutType *io = type->getAs<InOutType>()) {
15451545
Type objectType = io->getObjectType();
15461546
if (objectType->is<InOutType>()) {
1547-
Out << "type is an @inout of @inout type: ";
1547+
Out << "type is an inout of inout type: ";
15481548
type.print(Out);
15491549
Out << "\n";
15501550
}

Diff for: lib/IDE/CodeCompletion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ class CompletionLookup : public swift::VisibleDeclConsumer {
974974
// Add a type annotation.
975975
Type VarType = getTypeOfMember(VD);
976976
if (VD->getName() == Ctx.Id_self) {
977-
// Strip @inout from 'self'. It is useful to show @inout for function
977+
// Strip inout from 'self'. It is useful to show inout for function
978978
// parameters. But for 'self' it is just noise.
979979
VarType = VarType->getInOutObjectType();
980980
}

Diff for: lib/IRGen/GenMeta.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ namespace {
581581
llvm_unreachable("should have been lowered by SILGen");
582582
}
583583
llvm::Value *visitInOutType(CanInOutType type) {
584-
IGF.unimplemented(SourceLoc(), "metadata ref for @inout type");
584+
IGF.unimplemented(SourceLoc(), "metadata ref for inout type");
585585
return llvm::UndefValue::get(IGF.IGM.TypeMetadataPtrTy);
586586
}
587587

Diff for: lib/IRGen/GenPoly.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ namespace {
293293
llvm_unreachable("should have been lowered by SILGen");
294294
}
295295

296-
// @inout go by the object type; note that we ask the ordinary
296+
// inout go by the object type; note that we ask the ordinary
297297
// question, not the argument question.
298298
bool visitInOutType(CanInOutType origTy, CanInOutType substTy) {
299299
return differsByAbstractionInMemory(IGM,
@@ -591,7 +591,7 @@ namespace {
591591
CanType origObjectTy = origTy.getObjectType();
592592
CanType substObjectTy = substTy.getObjectType();
593593
if (differsByAbstractionInMemory(IGF.IGM, origObjectTy, substObjectTy))
594-
IGF.unimplemented(SourceLoc(), "remapping @inout values");
594+
IGF.unimplemented(SourceLoc(), "remapping inout values");
595595

596596
llvm::Value *substMV = In.claimNext();
597597
if (origObjectTy == substObjectTy)
@@ -762,7 +762,7 @@ namespace {
762762
CanType origObjectTy = origTy.getObjectType();
763763
CanType substObjectTy = substTy.getObjectType();
764764
if (differsByAbstractionInMemory(IGF.IGM, origObjectTy, substObjectTy))
765-
IGF.unimplemented(SourceLoc(), "remapping @inout");
765+
IGF.unimplemented(SourceLoc(), "remapping inout");
766766

767767
llvm::Value *origMV = In.claimNext();
768768
if (origObjectTy == substObjectTy)

Diff for: lib/IRGen/GenProto.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3380,7 +3380,7 @@ namespace {
33803380
llvm_unreachable("cannot store l-value type directly");
33813381
}
33823382
void visitInOutType(CanInOutType type) {
3383-
llvm_unreachable("cannot store @inout type directly");
3383+
llvm_unreachable("cannot store inout type directly");
33843384
}
33853385

33863386
// Bind archetypes from the parent of nominal types.

Diff for: lib/IRGen/GenType.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ TypeCacheEntry TypeConverter::convertType(CanType ty) {
10161016
llvm_unreachable("bad type kind");
10171017
}
10181018

1019-
/// Convert an @inout type. This is always just a bare pointer.
1019+
/// Convert an inout type. This is always just a bare pointer.
10201020
const TypeInfo *TypeConverter::convertInOutType(InOutType *T) {
10211021
auto referenceType =
10221022
IGM.getStoragePointerTypeForUnlowered(CanType(T->getObjectType()));

Diff for: lib/IRGen/IRGenDebugInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ llvm::DIType IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
12281228
}
12291229

12301230
case TypeKind::InOut: {
1231-
// This is an @inout type.
1231+
// This is an inout type.
12321232
auto ObjectTy = BaseTy->castTo<InOutType>()->getObjectType();
12331233
auto DT = getOrCreateDesugaredType(ObjectTy, DbgTy, Scope);
12341234
return DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, DT);

Diff for: lib/IRGen/IRGenSIL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2345,10 +2345,10 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
23452345

23462346
if (IGM.DebugInfo) {
23472347
auto Indirection = IndirectValue;
2348-
// LValues (@inout) are implicitly indirect because of their type.
2348+
// LValues are implicitly indirect because of their type.
23492349
if (Decl && Decl->getType()->getKind() == TypeKind::LValue)
23502350
Indirection = DirectValue;
2351-
// FIXME: @inout arguments that are not promoted are emitted as
2351+
// FIXME: inout arguments that are not promoted are emitted as
23522352
// arguments and also boxed and thus may show up twice. This may
23532353
// or may not be bad.
23542354
IGM.DebugInfo->emitStackVariableDeclaration

Diff for: lib/Parse/ParseDecl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1882,8 +1882,8 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, SourceLoc MutatingLoc,
18821882
// container type as an element named 'self'.
18831883
//
18841884
// This turns an instance function "(int)->int" on FooTy into
1885-
// "(this: @inout FooTy)->(int)->int", and a static function
1886-
// "(int)->int" on FooTy into "(this: @inout FooTy.metatype)->(int)->int".
1885+
// "(inout self: FooTy)->(int)->int", and a static function
1886+
// "(int)->int" on FooTy into "(inout self: FooTy.metatype)->(int)->int".
18871887
// Note that we can't actually compute the type here until Sema.
18881888
if (HasContainerType) {
18891889
Pattern *SelfPattern = buildImplicitSelfParameter(NameLoc, CurDeclContext);

Diff for: lib/Parse/ParseType.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
429429
/*AllowSepAfterLast=*/false,
430430
diag::expected_rparen_tuple_type_list,
431431
[&] () -> ParserStatus {
432+
// If this is an "inout" marker in an argument list, consume the inout.
433+
SourceLoc InOutLoc;
434+
if (Tok.isContextualKeyword("inout"))
435+
InOutLoc = consumeToken(tok::identifier);
436+
432437
// If the tuple element starts with "ident :", then
433438
// the identifier is an element tag, and it is followed by a type
434439
// annotation.
@@ -448,6 +453,14 @@ ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
448453
if (type.isNull())
449454
return makeParserError();
450455

456+
// If an 'inout' marker was specified, build the type. Note that we bury
457+
// the inout locator within the named locator. This is weird but required
458+
// by sema apparently.
459+
if (InOutLoc.isValid())
460+
type = makeParserResult(new (Context) InOutTypeRepr(type.get(),
461+
InOutLoc));
462+
463+
451464
ElementsR.push_back(
452465
new (Context) NamedTypeRepr(name, type.get(), nameLoc));
453466
} else {
@@ -457,6 +470,10 @@ ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
457470
return makeParserCodeCompletionStatus();
458471
if (type.isNull())
459472
return makeParserError();
473+
if (InOutLoc.isValid())
474+
type = makeParserResult(new (Context) InOutTypeRepr(type.get(),
475+
InOutLoc));
476+
460477
ElementsR.push_back(type.get());
461478
}
462479

Diff for: lib/SIL/SILFunctionType.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ namespace {
218218
/// B := Bool
219219
///
220220
/// But if we have a substituted function type:
221-
/// (UnicodeScalar, (Int, Float), @inout Double) -> Bool
221+
/// (UnicodeScalar, (Int, Float), inout Double) -> Bool
222222
/// then its most general form is
223-
/// (A, B, @inout C) -> D
223+
/// (A, B, inout C) -> D
224224
/// because the substitution
225-
/// X := (UnicodeScalar, (Int, Float), @inout Double)
226-
/// is invalid substitution, ultimately because '@inout Double'
225+
/// X := (UnicodeScalar, (Int, Float), inout Double)
226+
/// is invalid substitution, ultimately because 'inout Double'
227227
/// is not materializable.
228228
class DestructureGeneralizedInputs
229229
: public CanTypeVisitor<DestructureGeneralizedInputs> {
@@ -254,7 +254,7 @@ namespace {
254254
}
255255

256256
void visitInOutType(CanInOutType type) {
257-
// @inout types aren't valid targets for substitution.
257+
// inout types aren't valid targets for substitution.
258258
addInput(type.getObjectType(), ParameterConvention::Indirect_Inout);
259259
}
260260

Diff for: lib/SIL/TypeLowering.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ namespace {
200200
llvm_unreachable("shouldn't get an l-value type here");
201201
}
202202
RetTy visitInOutType(CanInOutType type) {
203-
llvm_unreachable("shouldn't get an @inout type here");
203+
llvm_unreachable("shouldn't get an inout type here");
204204
}
205205

206206
// Dependent types should be contextualized before visiting.
@@ -1190,7 +1190,7 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
11901190

11911191
assert(uncurryLevel == 0);
11921192

1193-
// @inout types are a special case for lowering, because they get
1193+
// inout types are a special case for lowering, because they get
11941194
// completely removed and represented as 'address' SILTypes.
11951195
if (auto substInOutType = dyn_cast<InOutType>(substType)) {
11961196
// Derive SILType for InOutType from the object type.
@@ -1590,7 +1590,7 @@ TypeConverter::getFunctionTypeWithCaptures(CanAnyFunctionType funcType,
15901590
SmallVector<TupleTypeElt, 8> inputFields;
15911591

15921592
for (ValueDecl *capture : captures) {
1593-
// A capture of a 'var' or '@inout' variable is done with the underlying
1593+
// A capture of a 'var' or 'inout' variable is done with the underlying
15941594
// object type.
15951595
auto captureType =
15961596
capture->getType()->getLValueOrInOutObjectType()->getCanonicalType();
@@ -1672,7 +1672,7 @@ TypeConverter::getFunctionInterfaceTypeWithCaptures(CanAnyFunctionType funcType,
16721672
SmallVector<TupleTypeElt, 8> inputFields;
16731673

16741674
for (ValueDecl *capture : captures) {
1675-
// A capture of a 'var' or '@inout' variable is done with the underlying
1675+
// A capture of a 'var' or 'inout' variable is done with the underlying
16761676
// object type.
16771677
auto captureType =
16781678
capture->getType()->getLValueOrInOutObjectType()->getCanonicalType();

0 commit comments

Comments
 (0)