Skip to content

[SR-511][Parse] Add 'associatedtype' keyword and fixit#964

Merged
DougGregor merged 1 commit intoswiftlang:masterfrom
gregomni:sr-511
Jan 14, 2016
Merged

[SR-511][Parse] Add 'associatedtype' keyword and fixit#964
DougGregor merged 1 commit intoswiftlang:masterfrom
gregomni:sr-511

Conversation

@gregomni
Copy link
Contributor

Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new test for the fixit
is at the end of Generics/associated_types.swift.

There is one open question at the moment, which is what should happen
with using associatedtype in a non-Protocol context:

protocol P9 {
  associatedtype A1 : P8
}
struct Y9 : P9 {
  typealias A1 = X8
}

Currently the code still expects typealias in the struct, and it is a
typealias as far as the compiler is concerned. But I can certainly
imagine users mistakenly thinking that they are ‘assigning’ a specific type to an
associatedtype instead. With this current commit, using the
associatedtype keyword there will assert an unexpected token. It should
either accept it or have a nice error, and I’m not sure which.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we tighten this wording up a bit, and quote the keywords? Perhaps:

"use of 'typealias' to declare associated types is deprecated; use 'associatedtype' instead"

?

@DougGregor
Copy link
Member

I think that a specific diagnostic for the case of writing 'associatedtype' in a non-protocol would be useful. Something like

associated types can only be defined in a protocol; define a type or introduce a 'typealias' to satisfy an associated type requirement

@DougGregor
Copy link
Member

Otherwise, this looks great!

@gregomni
Copy link
Contributor Author

@DougGregor Thanks for the better language on the diagnoses! Committed those fixes.

@DougGregor
Copy link
Member

Can you squash these and then I'll merge?

@dabrahams
Copy link
Contributor

On Jan 13, 2016, at 3:06 PM, Greg Titus notifications@github.com wrote:

Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new test for the fixit
is at the end of Generics/associated_types.swift.

There is one open question at the moment, which is what should happen
with using associatedtype in a non-Protocol context:

protocol P9 {
associatedtype A1 : P8
}
struct Y9 : P9 {
typealias A1 = X8
}
Currently the code still expects typealias in the struct, and it is a
typealias as far as the compiler is concerned.

The code should not be requiring a typealias; it never did before. This should work:

struct Y9 : P9 {
struct A1 : P8 {}
}

But I can certainly
imagine users mistakenly thinking that they are ‘assigning’ a specific type to an
associatedtype instead. With this current commit, using the
associatedtype keyword there will assert an unexpected token. It should
either accept it or have a nice error, and I’m not sure which.

You can view, comment on, or merge this pull request online at:

#964 #964
Commit Summary

[SR-511][Parse] Add 'associatedtype' keyword and fixit
File Changes

M include/swift/AST/DiagnosticsParse.def https://github.com/apple/swift/pull/964/files#diff-0 (4)
M include/swift/IDE/CodeCompletion.h https://github.com/apple/swift/pull/964/files#diff-1 (1)
M include/swift/Parse/Tokens.def https://github.com/apple/swift/pull/964/files#diff-2 (1)
M lib/AST/ASTPrinter.cpp https://github.com/apple/swift/pull/964/files#diff-3 (2)
M lib/IDE/CodeCompletion.cpp https://github.com/apple/swift/pull/964/files#diff-4 (7)
M lib/IDE/REPLCodeCompletion.cpp https://github.com/apple/swift/pull/964/files#diff-5 (1)
M lib/Parse/ParseDecl.cpp https://github.com/apple/swift/pull/964/files#diff-6 (17)
M test/Constraints/associated_types.swift https://github.com/apple/swift/pull/964/files#diff-7 (2)
M test/Constraints/diagnostics.swift https://github.com/apple/swift/pull/964/files#diff-8 (3)
M test/Constraints/generic_overload.swift https://github.com/apple/swift/pull/964/files#diff-9 (4)
M test/Constraints/generics.swift https://github.com/apple/swift/pull/964/files#diff-10 (4)
M test/Constraints/invalid_archetype_constraint.swift https://github.com/apple/swift/pull/964/files#diff-11 (2)
M test/Constraints/invalid_constraint_lookup.swift https://github.com/apple/swift/pull/964/files#diff-12 (6)
M test/Constraints/patterns.swift https://github.com/apple/swift/pull/964/files#diff-13 (2)
M test/Constraints/same_types.swift https://github.com/apple/swift/pull/964/files#diff-14 (4)
M test/Generics/Inputs/associated_types_multi_file_helper.swift https://github.com/apple/swift/pull/964/files#diff-15 (2)
M test/Generics/associated_self_constraints.swift https://github.com/apple/swift/pull/964/files#diff-16 (6)
M test/Generics/associated_type_typo.swift https://github.com/apple/swift/pull/964/files#diff-17 (4)
M test/Generics/associated_types.swift https://github.com/apple/swift/pull/964/files#diff-18 (27)
M test/Generics/associated_types_inherit.swift https://github.com/apple/swift/pull/964/files#diff-19 (2)
M test/Generics/deduction.swift https://github.com/apple/swift/pull/964/files#diff-20 (2)
M test/Generics/function_defs.swift https://github.com/apple/swift/pull/964/files#diff-21 (26)
M test/Generics/generic_types.swift https://github.com/apple/swift/pull/964/files#diff-22 (4)
M test/Generics/requirement_inference.swift https://github.com/apple/swift/pull/964/files#diff-23 (22)
M test/Generics/same_type_constraints.swift https://github.com/apple/swift/pull/964/files#diff-24 (34)
M test/IDE/complete_associated_types.swift https://github.com/apple/swift/pull/964/files#diff-25 (126)
M test/IDE/complete_enum_elements.swift https://github.com/apple/swift/pull/964/files#diff-26 (4)
M test/IDE/complete_value_expr.swift https://github.com/apple/swift/pull/964/files#diff-27 (2)
M test/IDE/print_ast_tc_decls.swift https://github.com/apple/swift/pull/964/files#diff-28 (24)
M test/IDE/print_ast_tc_decls_errors.swift https://github.com/apple/swift/pull/964/files#diff-29 (26)
M test/IDE/print_types.swift https://github.com/apple/swift/pull/964/files#diff-30 (2)
M test/IDE/print_usrs.swift https://github.com/apple/swift/pull/964/files#diff-31 (4)
M test/NameBinding/accessibility.swift https://github.com/apple/swift/pull/964/files#diff-32 (2)
M test/NameBinding/name_lookup.swift https://github.com/apple/swift/pull/964/files#diff-33 (2)
M test/NameBinding/stdlib.swift https://github.com/apple/swift/pull/964/files#diff-34 (2)
M test/Parse/type_expr.swift https://github.com/apple/swift/pull/964/files#diff-35 (2)
M test/SILGen/errors.swift https://github.com/apple/swift/pull/964/files#diff-36 (2)
M test/Sema/accessibility.swift https://github.com/apple/swift/pull/964/files#diff-37 (30)
M test/Sema/availability_versions.swift https://github.com/apple/swift/pull/964/files#diff-38 (2)
M test/Sema/circular_decl_checking.swift https://github.com/apple/swift/pull/964/files#diff-39 (4)
M test/Sema/diag_values_of_module_type.swift https://github.com/apple/swift/pull/964/files#diff-40 (2)
M test/SourceKit/CodeComplete/complete_override.swift.response https://github.com/apple/swift/pull/964/files#diff-41 (9)
M test/SourceKit/DocSupport/Inputs/cake.swift https://github.com/apple/swift/pull/964/files#diff-42 (2)
M test/SourceKit/DocSupport/Inputs/main.swift https://github.com/apple/swift/pull/964/files#diff-43 (2)
M test/SourceKit/DocSupport/doc_source_file.swift.response https://github.com/apple/swift/pull/964/files#diff-44 (110)
M test/SourceKit/DocSupport/doc_swift_module.swift.response https://github.com/apple/swift/pull/964/files#diff-45 (106)
M test/attr/accessibility.swift https://github.com/apple/swift/pull/964/files#diff-46 (8)
M test/attr/accessibility_print.swift https://github.com/apple/swift/pull/964/files#diff-47 (14)
M test/attr/attr_autoclosure.swift https://github.com/apple/swift/pull/964/files#diff-48 (4)
M test/attr/attr_noescape.swift https://github.com/apple/swift/pull/964/files#diff-49 (4)
M test/decl/ext/extensions.swift https://github.com/apple/swift/pull/964/files#diff-50 (2)
M test/decl/ext/generic.swift https://github.com/apple/swift/pull/964/files#diff-51 (6)
M test/decl/ext/protocol.swift https://github.com/apple/swift/pull/964/files#diff-52 (32)
M test/decl/nested.swift https://github.com/apple/swift/pull/964/files#diff-53 (10)
M test/decl/protocol/conforms/associated_type.swift https://github.com/apple/swift/pull/964/files#diff-54 (2)
M test/decl/protocol/conforms/failure.swift https://github.com/apple/swift/pull/964/files#diff-55 (10)
M test/decl/protocol/conforms/inherited.swift https://github.com/apple/swift/pull/964/files#diff-56 (2)
M test/decl/protocol/indirectly_recursive_requirement.swift https://github.com/apple/swift/pull/964/files#diff-57 (4)
M test/decl/protocol/protocol_overload_selection.swift https://github.com/apple/swift/pull/964/files#diff-58 (6)
M test/decl/protocol/protocols.swift https://github.com/apple/swift/pull/964/files#diff-59 (18)
M test/decl/protocol/recursive_requirement.swift https://github.com/apple/swift/pull/964/files#diff-60 (18)
M test/decl/protocol/req/associated_type_default.swift https://github.com/apple/swift/pull/964/files#diff-61 (6)
M test/decl/protocol/req/associated_type_inference.swift https://github.com/apple/swift/pull/964/files#diff-62 (40)
M test/decl/protocol/req/func.swift https://github.com/apple/swift/pull/964/files#diff-63 (10)
M test/decl/protocol/req/optional.swift https://github.com/apple/swift/pull/964/files#diff-64 (2)
M test/decl/protocol/req/recursion.swift https://github.com/apple/swift/pull/964/files#diff-65 (6)
M test/decl/subscript/subscripting.swift https://github.com/apple/swift/pull/964/files#diff-66 (2)
M test/decl/typealias/associated_types.swift https://github.com/apple/swift/pull/964/files#diff-67 (2)
M test/decl/typealias/dependent_types.swift https://github.com/apple/swift/pull/964/files#diff-68 (4)
M test/decl/var/properties.swift https://github.com/apple/swift/pull/964/files#diff-69 (2)
M test/type/protocol_types.swift https://github.com/apple/swift/pull/964/files#diff-70 (2)
M tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp https://github.com/apple/swift/pull/964/files#diff-71 (1)
M tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp https://github.com/apple/swift/pull/964/files#diff-72 (6)
Patch Links:

https://github.com/apple/swift/pull/964.patch https://github.com/apple/swift/pull/964.patch
https://github.com/apple/swift/pull/964.diff https://github.com/apple/swift/pull/964.diff

Reply to this email directly or view it on GitHub #964.

-Dave

Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new tests for the fixits
is at the end of Generics/associated_types.swift.
@gregomni
Copy link
Contributor Author

@dabrahams No, the typealias isn't required at all, but it does appear to be a fairly common pattern to write something like it - @DougGregor 's suggested diagnosis text is great for telling people it isn't needed.

Also: squashed!

DougGregor added a commit that referenced this pull request Jan 14, 2016
[SR-511][Parse] Add 'associatedtype' keyword and fixit
@DougGregor DougGregor merged commit 8170f8b into swiftlang:master Jan 14, 2016
@gregomni gregomni deleted the sr-511 branch January 14, 2016 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants