Skip to content

Commit 67dccb2

Browse files
joewillsherDougGregor
authored andcommitted
[SE-0095] Code feedback changes; Any is parsed as a keyword
- Any is made into a keyword which is always resolved into a TypeExpr, allowing the removal of the type system code to find TheAnyType before an unconstrained lookup. - Types called `Any` can be declared, they are looked up as any other identifier is - Renaming/redefining behaviour of source loc methods on ProtocolCompositionTypeRepr. Added a createEmptyComposition static method too. - Code highlighting treats Any as a type - simplifyTypeExpr also does not rely on source to get operator name. - Any is now handled properly in canParseType() which was causing generic param lists containing ‘Any’ to fail - The import objc id as Any work has been relying on getting a decl for the Any type. I fix up the clang importer to use Context.TheAnyType (instead of getAnyDecl()->getDeclaredType()). When importing the id typedef, we create a typealias to Any and declare it unavaliable.
1 parent da94183 commit 67dccb2

36 files changed

+264
-2188
lines changed

CHANGELOG.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ Swift 3.0
44
---------
55

66
* [SE-0095](https://github.com/apple/swift-evolution/blob/master/proposals/0095-any-as-existential.md):
7-
The `protocol<...>` composition construct has been removed. In its
8-
place, an infix type operator `&` has been introduced.
7+
The `protocol<...>` composition construct has been removed. In its
8+
place, an infix type operator `&` has been introduced.
99

10-
```swift
11-
let a: Foo & Bar
12-
let b = value as? A & B & C
13-
func foo<T : Foo & Bar>(x: T) { }
14-
func bar(x: Foo & Bar) { }
15-
typealias G = GenericStruct<Foo & Bar>
16-
```
10+
```swift
11+
let a: Foo & Bar
12+
let b = value as? A & B & C
13+
func foo<T : Foo & Bar>(x: T) { }
14+
func bar(x: Foo & Bar) { }
15+
typealias G = GenericStruct<Foo & Bar>
16+
```
1717

18-
The empty protocol composition, the `Any` type, was previously
19-
defined as being `protocol<>`. This has been removed from the
20-
standard library and `Any` is now a keyword with the same behaviour.
18+
The empty protocol composition, the `Any` type, was previously
19+
defined as being `protocol<>`. This has been removed from the
20+
standard library and `Any` is now a keyword with the same behaviour.
2121

2222
* [SE-0091](https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md):
2323
Operators can now be defined within types or extensions thereof. For example:

docs/ABI.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ contain the following fields:
461461
`protocol descriptor`_ records, but are pre-calculated for convenience.
462462

463463
- The **number of protocols** that make up the protocol composition is stored at
464-
**offset 2**. For the "any" types ``Any`` or ``Any : Class``, this
464+
**offset 2**. For the "any" types ``Any`` or ``Any : class``, this
465465
is zero. For a single-protocol type ``P``, this is one. For a protocol
466466
composition type ``P & Q & ...``, this is the number of protocols.
467467

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,9 @@ ERROR(expected_rangle_protocol,PointsToFirstBadToken,
655655
ERROR(disallowed_protocol_composition,PointsToFirstBadToken,
656656
"protocol composition is neither allowed nor needed here", ())
657657

658-
WARNING(deprecated_protocol_composition,PointsToFirstBadToken,
658+
WARNING(deprecated_protocol_composition,none,
659659
"'protocol<...>' composition syntax is deprecated; join the protocols using '&'", ())
660-
WARNING(deprecated_any_composition,PointsToFirstBadToken,
660+
WARNING(deprecated_any_composition,none,
661661
"'protocol<>' syntax is deprecated; use 'Any' instead", ())
662662

663663
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ ERROR(reserved_member_name,none,
517517
ERROR(invalid_redecl,none,"invalid redeclaration of %0", (DeclName))
518518
NOTE(invalid_redecl_prev,none,
519519
"%0 previously declared here", (DeclName))
520-
ERROR(invalid_redecl_any,none,"invalid redeclaration of 'Any'; cannot overload type keyword", ())
521520

522521
ERROR(ambiguous_type_base,none,
523522
"%0 is ambiguous for type lookup in this context", (Identifier))

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
IDENTIFIER(alloc)
2626
IDENTIFIER(allocWithZone)
2727
IDENTIFIER(allZeros)
28+
IDENTIFIER(Any)
2829
IDENTIFIER(atIndexedSubscript)
2930
IDENTIFIER_(bridgeToObjectiveC)
3031
IDENTIFIER_WITH_NAME(code_, "_code")
@@ -60,7 +61,6 @@ IDENTIFIER(RawValue)
6061
IDENTIFIER(Selector)
6162
IDENTIFIER(self)
6263
IDENTIFIER(Self)
63-
IDENTIFIER(Any)
6464
IDENTIFIER(setObject)
6565
IDENTIFIER(simd)
6666
IDENTIFIER(some)

include/swift/AST/TypeRepr.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,21 +629,27 @@ class ProtocolCompositionTypeRepr : public TypeRepr {
629629
}
630630

631631
ArrayRef<IdentTypeRepr *> getProtocols() const { return Protocols; }
632-
SourceLoc getStartLoc() const { return FirstTypeLoc; }
632+
SourceLoc getSourceLoc() const { return FirstTypeLoc; }
633633
SourceRange getCompositionRange() const { return CompositionRange; }
634634

635635
static ProtocolCompositionTypeRepr *create(ASTContext &C,
636636
ArrayRef<IdentTypeRepr*> Protocols,
637637
SourceLoc FirstTypeLoc,
638638
SourceRange CompositionRange);
639-
639+
640+
static ProtocolCompositionTypeRepr *createEmptyComposition(ASTContext &C,
641+
SourceLoc AnyLoc) {
642+
return ProtocolCompositionTypeRepr::create(C, {}, AnyLoc, {AnyLoc, AnyLoc});
643+
}
644+
640645
static bool classof(const TypeRepr *T) {
641646
return T->getKind() == TypeReprKind::ProtocolComposition;
642647
}
643648
static bool classof(const ProtocolCompositionTypeRepr *T) { return true; }
644649

645650
private:
646651
SourceLoc getStartLocImpl() const { return FirstTypeLoc; }
652+
SourceLoc getLocImpl() const { return CompositionRange.Start; }
647653
SourceLoc getEndLocImpl() const { return CompositionRange.End; }
648654
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
649655
friend class TypeRepr;

include/swift/Parse/Parser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,9 @@ class Parser {
877877
SourceLoc &LAngleLoc,
878878
SourceLoc &RAngleLoc);
879879

880-
ParserResult<IdentTypeRepr> parseTypeIdentifier();
880+
ParserResult<TypeRepr> parseTypeIdentifier();
881881
ParserResult<TypeRepr> parseTypeIdentifierOrTypeComposition();
882+
ParserResult<ProtocolCompositionTypeRepr> parseAnyType();
882883

883884
ParserResult<TupleTypeRepr> parseTypeTupleBody();
884885
ParserResult<TypeRepr> parseTypeArray(TypeRepr *Base);
@@ -1083,7 +1084,7 @@ class Parser {
10831084
bool canParseType();
10841085
bool canParseTypeIdentifier();
10851086
bool canParseTypeIdentifierOrTypeComposition();
1086-
bool canParseTypeComposition();
1087+
bool canParseOldStyleProtocolComposition();
10871088
bool canParseTypeTupleBody();
10881089
bool canParseTypeAttribute();
10891090
bool canParseGenericArguments();

include/swift/Parse/Tokens.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ STMT_KEYWORD(catch)
128128

129129
// Expression keywords.
130130
KEYWORD(as)
131+
KEYWORD(Any)
131132
KEYWORD(dynamicType)
132133
KEYWORD(false)
133134
KEYWORD(is)

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bool TypeBase::hasReferenceSemantics() {
9090
}
9191

9292
bool TypeBase::isAny() {
93-
return isEqual(getASTContext().getAnyDecl()->getDeclaredType());
93+
return isEqual(getASTContext().TheAnyType);
9494
}
9595

9696
bool TypeBase::isAnyClassReferenceType() {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,22 @@ namespace {
15351535
if (Name.str() == "id" && Impl.SwiftContext.LangOpts.EnableIdAsAny) {
15361536
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
15371537
MappedTypeNameKind::DoNothing;
1538-
return Impl.SwiftContext.getAnyDecl();
1538+
1539+
auto DC = Impl.importDeclContextOf(Decl, importedName.EffectiveContext);
1540+
if (!DC) return nullptr;
1541+
1542+
auto loc = Impl.importSourceLoc(Decl->getLocStart());
1543+
auto Result = Impl.createDeclWithClangNode<TypeAliasDecl>(
1544+
Decl, loc, Name, loc,
1545+
TypeLoc::withoutLoc(Impl.SwiftContext.TheAnyType),
1546+
/*genericparams*/nullptr, DC);
1547+
1548+
auto attr = AvailableAttr::createUnconditional(
1549+
Impl.SwiftContext, "'id' is not available in Swift; use 'Any'",
1550+
"", UnconditionalAvailabilityKind::UnavailableInSwift);
1551+
1552+
Result->getAttrs().add(attr);
1553+
return Result;
15391554
}
15401555

15411556
bool IsError;

0 commit comments

Comments
 (0)