Skip to content

Commit e05649d

Browse files
committed
[Sema] Properly diagnose Optional lowercase rename of None/Some
Because Optional static var .None and static func .Some unavailable stubs that were put in the standard library aren't imported from C/Objective-C, the path that diagnoses renaming enum cases to lowercase isn't triggered. rdar://problem/26481304
1 parent 32f8153 commit e05649d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "TypeChecker.h"
1919
#include "GenericTypeResolver.h"
20+
#include "swift/Basic/StringExtras.h"
2021
#include "swift/AST/ExprHandle.h"
2122
#include "swift/AST/ASTWalker.h"
2223
#include "swift/AST/ASTVisitor.h"
@@ -1401,9 +1402,24 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
14011402
EEP->getLoc());
14021403
}
14031404
if (!elt) {
1404-
if (!type->is<ErrorType>())
1405+
if (!type->is<ErrorType>()) {
1406+
if (type->getAnyNominal() == Context.getOptionalDecl()) {
1407+
if (EEP->getName().str().compare("None") == 0 ||
1408+
EEP->getName().str().compare("Some") == 0) {
1409+
SmallString<4> Rename;
1410+
camel_case::toLowercaseWord(EEP->getName().str(), Rename);
1411+
diagnose(EEP->getLoc(), diag::availability_decl_unavailable_rename,
1412+
EEP->getName(), /*replaced*/false,
1413+
/*special kind*/0, Rename.str())
1414+
.fixItReplace(EEP->getLoc(), Rename.str());
1415+
1416+
}
1417+
return true;
1418+
}
1419+
14051420
diagnose(EEP->getLoc(), diag::enum_element_pattern_member_not_found,
14061421
EEP->getName().str(), type);
1422+
}
14071423
return true;
14081424
}
14091425
enumTy = type;

test/1_stdlib/OptionalRenames.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
func getInt(x: Int) -> Int? {
4+
if x == 0 {
5+
return .None // expected-error {{'None' has been renamed to 'none'}} {{13-17=none}}
6+
}
7+
return .Some(1) // expected-error {{'Some' has been renamed to 'some'}} {{11-15=some}}
8+
}
9+
10+
let x = Optional.Some(1) // expected-error {{'Some' has been renamed to 'some'}} {{18-22=some}}
11+
12+
switch x {
13+
case .None: break // expected-error {{'None' has been renamed to 'none'}} {{9-13=none}}
14+
case .Some(let x): print(x) // expected-error {{'Some' has been renamed to 'some'}} {{9-13=some}}
15+
}
16+

0 commit comments

Comments
 (0)