Skip to content

Add a fixit for attempting member lookup on Any. #4078

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
Aug 7, 2016
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ ERROR(could_not_find_type_member,none,
NOTE(did_you_mean_raw_type,none,
"did you mean to specify a raw type on the enum declaration?", ())

NOTE(any_as_anyobject_fixit, none,
"cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members", ())

ERROR(expected_argument_in_contextual_member,none,
"contextual member %0 expects argument of type %1", (DeclName, Type))

Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,14 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
diagnose(loc, diag::did_you_mean_raw_type);
return; // Always prefer this over typo corrections.
}
} else if (baseObjTy->isAny()) {
if (auto DRE = dyn_cast<DeclRefExpr>(baseExpr)) {
auto name = DRE->getDecl()->getName().str().str();
std::string replacement = "(" + name + " as AnyObject)";
diagnose(loc, diag::any_as_anyobject_fixit)
.fixItReplace(baseExpr->getSourceRange(), replacement);
return;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/Constraints/dynamic_lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,9 @@ uopt.wibble!()
// Should not be able to see private or internal @objc methods.
uopt.privateFoo!() // expected-error{{'privateFoo' is inaccessible due to 'private' protection level}}
uopt.internalFoo!() // expected-error{{'internalFoo' is inaccessible due to 'internal' protection level}}

let anyValue: Any = X()
_ = anyValue.bar() // expected-error {{value of type 'Any' has no member 'bar'}}
// expected-note@-1 {{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}}{{5-13=(anyValue as AnyObject)}}
_ = (anyValue as AnyObject).bar()
_ = (anyValue as! X).bar()
3 changes: 3 additions & 0 deletions test/Constraints/tuple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ any = (label: 4)
// Scalars don't have .0/.1/etc
i = j.0 // expected-error{{value of type 'Int' has no member '0'}}
any.1 // expected-error{{value of type 'Any' has no member '1'}}
// expected-note@-1{{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}}
any = (5.0, 6.0) as (Float, Float)
_ = (any as! (Float, Float)).1

// Fun with tuples
protocol PosixErrorReturn {
Expand Down