Skip to content

IUO: Set the IUO attribute on ParamDecls for accessors arguments. #14214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3547,9 +3547,10 @@ static AccessorDecl *createAccessorFunc(SourceLoc DeclLoc,
return D;
}

static ParamDecl *
createSetterAccessorArgument(SourceLoc nameLoc, Identifier name,
AccessorKind accessorKind, Parser &P) {
static ParamDecl *createSetterAccessorArgument(SourceLoc nameLoc,
Identifier name,
AccessorKind accessorKind,
Parser &P, TypeLoc elementType) {
// Add the parameter. If no name was specified, the name defaults to
// 'value'.
bool isNameImplicit = name.empty();
Expand All @@ -3568,15 +3569,25 @@ createSetterAccessorArgument(SourceLoc nameLoc, Identifier name,

// AST Walker shouldn't go into the type recursively.
result->setIsTypeLocImplicit(true);

if (auto *repr = elementType.getTypeRepr()) {
if (repr->getKind() ==
TypeReprKind::ImplicitlyUnwrappedOptional) {
result->getAttrs().add(
new (P.Context) ImplicitlyUnwrappedOptionalAttr(/* implicit= */ true));
}
}

return result;
}

/// Parse a "(value)" specifier for "set" or "willSet" if present. Create a
/// parameter list to represent the spelled argument or return null if none is
/// present.
static ParameterList *
parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
Parser &P, AccessorKind Kind) {
static ParameterList *parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
Parser &P,
AccessorKind Kind,
TypeLoc ElementTy) {
// 'set' and 'willSet' have a (value) parameter, 'didSet' takes an (oldValue)
// parameter and 'get' and always takes a () parameter.
if (Kind != AccessorKind::IsSetter && Kind != AccessorKind::IsWillSet &&
Expand Down Expand Up @@ -3614,7 +3625,7 @@ parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
}

if (Name.empty()) NameLoc = SpecifierLoc;
auto param = createSetterAccessorArgument(NameLoc, Name, Kind, P);
auto param = createSetterAccessorArgument(NameLoc, Name, Kind, P, ElementTy);
return ParameterList::create(P.Context, StartLoc, param, EndLoc);
}

Expand Down Expand Up @@ -3846,8 +3857,8 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags,
if (Tok.is(tok::l_paren))
diagnose(Loc, diag::protocol_setter_name);

auto *ValueNameParams
= parseOptionalAccessorArgument(Loc, *this, Kind);
auto *ValueNameParams =
parseOptionalAccessorArgument(Loc, *this, Kind, ElementTy);

// Set up a function declaration.
TheDecl = createAccessorFunc(Loc, ValueNameParams,
Expand Down Expand Up @@ -3970,7 +3981,7 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags,
//
// set-name ::= '(' identifier ')'
auto *ValueNamePattern =
parseOptionalAccessorArgument(Loc, *this, Kind);
parseOptionalAccessorArgument(Loc, *this, Kind, ElementTy);

SyntaxParsingContext BlockCtx(SyntaxContext, SyntaxKind::CodeBlock);
if (AccessorKeywordLoc.isInvalid()) {
Expand Down Expand Up @@ -4426,8 +4437,8 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
auto argFunc = (WillSet ? WillSet : DidSet);
auto argLoc = argFunc->getParameterLists().back()->getStartLoc();

auto argument = createSetterAccessorArgument(argLoc, Identifier(),
AccessorKind::IsSetter, P);
auto argument = createSetterAccessorArgument(
argLoc, Identifier(), AccessorKind::IsSetter, P, elementTy);
auto argList = ParameterList::create(P.Context, argument);
Set = createImplicitAccessor(AccessorKind::IsSetter,
AddressorKind::NotAddressor, argList);
Expand All @@ -4448,8 +4459,8 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
if (MutableAddressor) {
assert(Get && !Set);
auto argument =
createSetterAccessorArgument(MutableAddressor->getLoc(), Identifier(),
AccessorKind::IsSetter, P);
createSetterAccessorArgument(MutableAddressor->getLoc(), Identifier(),
AccessorKind::IsSetter, P, elementTy);
auto argList = ParameterList::create(P.Context, argument);
Set = createImplicitAccessor(AccessorKind::IsSetter,
AddressorKind::NotAddressor, argList);
Expand Down
23 changes: 22 additions & 1 deletion test/Constraints/iuo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@ struct S {
var j: Int!
let k: Int
var m: Int
var n: Int! {
get {
return m
}
set {
m = newValue
}
}
var o: Int! {
willSet {
m = newValue
}
didSet {
m = oldValue
}
}

func fn() -> Int! { return i }

static func static_fn() -> Int! { return 0 }

subscript(i: Int) -> Int! {
return i
set {
m = newValue
}
get {
return i
}
}

init(i: Int!, j: Int!, k: Int, m: Int) {
Expand Down