Skip to content

Commit a1e8d29

Browse files
committed
implementing changes and fixing unit tests
1 parent b36568e commit a1e8d29

File tree

7 files changed

+24
-6
lines changed

7 files changed

+24
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@ NOTE(single_confusable_character,none,
10481048
(bool, StringRef, StringRef, StringRef, StringRef))
10491049
ERROR(cannot_find_type_in_scope,none,
10501050
"cannot find type %0 in scope", (DeclNameRef))
1051+
ERROR(cannot_find_type_in_cast_expression,none,
1052+
"cast expression expects a type on its right-hand side (got: %0)", (DeclNameRef))
10511053
ERROR(cannot_find_type_in_scope_did_you_mean,none,
10521054
"cannot find type %0 in scope; did you mean to use '%1'?", (DeclNameRef, StringRef))
10531055
NOTE(note_typo_candidate_implicit_member,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,12 @@ static Type diagnoseUnknownType(TypeResolution resolution,
12941294

12951295
// Unqualified lookup case.
12961296
if (parentType.isNull()) {
1297+
if (resolution.getOptions().hasBase(TypeResolverContext::ExplicitCastExpr)) {
1298+
diags.diagnose(repr->getNameLoc(), diag::cannot_find_type_in_cast_expression,
1299+
repr->getNameRef());
1300+
return ErrorType::get(ctx);
1301+
}
1302+
12971303
// Tailored diagnostic for custom attributes.
12981304
if (resolution.getOptions().is(TypeResolverContext::CustomAttr)) {
12991305
diags.diagnose(repr->getNameLoc(), diag::unknown_attribute,

test/ClangImporter/Darwin_test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import Darwin
66
import MachO
77

8-
_ = nil as Fract? // expected-error{{cannot find type 'Fract' in scope}}
8+
_ = nil as Fract? // expected-error{{cast expression expects a type on its right-hand side (got: 'Fract')}}
99
_ = nil as Darwin.Fract? // okay
1010

1111
_ = 0 as OSErr
@@ -14,7 +14,7 @@ _ = 0 as UniChar
1414

1515
_ = ProcessSerialNumber()
1616

17-
_ = 0 as Byte // expected-error {{cannot find type 'Byte' in scope}} {{10-14=UInt8}}
17+
_ = 0 as Byte // expected-error {{cast expression expects a type on its right-hand side (got: 'Byte')}}
1818
Darwin.fakeAPIUsingByteInDarwin() as Int // expected-error {{cannot convert value of type 'UInt8' to type 'Int' in coercion}}
1919

2020
_ = FALSE // expected-error {{cannot find 'FALSE' in scope}}

test/Constraints/casts.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,13 @@ do {
741741
assert(Set(b.map { $0 as A }) == Set(a)) // Ok
742742
}
743743
}
744+
745+
// https://github.com/apple/swift/issues/68825
746+
do {
747+
func x(a: Any) {
748+
_ = a is a // expected-error {{cast expression expects a type on its right-hand side (got: 'a')}}
749+
_ = a is a? // expected-error {{cast expression expects a type on its right-hand side (got: 'a')}}
750+
_ = a as T // expected-error {{cast expression expects a type on its right-hand side (got: 'T')}}
751+
_ = a is String // OK
752+
}
753+
}

test/Parse/omit_return.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ class CImplicitIdentityExpr { func gimme() -> CImplicitIdentityExpr { self } }
17101710
class CImplicitDotSelfExpr { func gimme() -> CImplicitDotSelfExpr { self.self } }
17111711

17121712
func badIs<T>(_ value: Any, anInstanceOf type: T.Type) -> Bool {
1713-
value is type // expected-error {{cannot find type 'type' in scope}}
1713+
value is type // expected-error {{cast expression expects a type on its right-hand side (got: 'type')}}
17141714
}
17151715

17161716

test/Parse/omit_return_fail.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-frontend %s -typecheck -verify
22

33
func badIs<T>(_ value: Any, anInstanceOf type: T.Type) -> Bool {
4-
value is type // expected-error {{cannot find type 'type' in scope}}
4+
value is type // expected-error {{cast expression expects a type on its right-hand side (got: 'type')}}
55
}
66

77
func foo() -> Int {
@@ -10,7 +10,7 @@ func foo() -> Int {
1010

1111
func badIs_ifdecl<T>(_ value: Any, anInstanceOf type: T.Type) -> Bool {
1212
#if true
13-
value is type // expected-error {{cannot find type 'type' in scope}}
13+
value is type // expected-error {{cast expression expects a type on its right-hand side (got: 'type')}}
1414
#endif
1515
}
1616

test/Parse/omit_return_ifdecl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ class CImplicitDotSelfExpr { func gimme() -> CImplicitDotSelfExpr { self.self }
24322432

24332433
func badIs<T>(_ value: Any, anInstanceOf type: T.Type) -> Bool {
24342434
#if true
2435-
value is type // expected-error {{cannot find type 'type' in scope}}
2435+
value is type // expected-error {{cast expression expects a type on its right-hand side (got: 'type')}}
24362436
#endif
24372437
}
24382438

0 commit comments

Comments
 (0)