Skip to content

Commit 55a97dd

Browse files
committed
Provide fix-its when overriding a bridged value type with the old reference type (#2479)
Merge pull request #2479 from jrose-apple/override-value-types-fix-it
2 parents 7362123 + 7bfdd4a commit 55a97dd

File tree

19 files changed

+342
-26
lines changed

19 files changed

+342
-26
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4193,7 +4193,8 @@ class VarDecl : public AbstractStorageDecl {
41934193
return VarDeclBits.IsUserAccessible;
41944194
}
41954195

4196-
/// \brief Retrieve the source range of the variable type.
4196+
/// Retrieve the source range of the variable type, or an invalid range if the
4197+
/// variable's type is not explicitly written in the source.
41974198
///
41984199
/// Only for use in diagnostics. It is not always possible to always
41994200
/// precisely point to the variable type because of type aliases.

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,12 @@ NOTE(subscript_override_here,none,
14601460
"attempt to override subscript here", ())
14611461
NOTE(convenience_init_override_here,none,
14621462
"attempt to override convenience initializer here", ())
1463+
NOTE(override_type_mismatch_with_fixits,none,
1464+
"type does not match superclass %0 with type %1",
1465+
(DescriptiveDeclKind, Type))
1466+
NOTE(override_type_mismatch_with_fixits_init,none,
1467+
"type does not match superclass initializer with %select{no arguments|argument %1|arguments %1}0",
1468+
(unsigned, Type))
14631469
ERROR(override_nonclass_decl,none,
14641470
"'override' can only be specified on class members", ())
14651471
ERROR(override_property_type_mismatch,none,

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ IDENTIFIER_(nsErrorDomain)
5151
IDENTIFIER(objectAtIndexedSubscript)
5252
IDENTIFIER(objectForKeyedSubscript)
5353
IDENTIFIER(ObjectiveC)
54+
IDENTIFIER_(ObjectiveCType)
5455
IDENTIFIER_(OptionalNilComparisonType)
5556
IDENTIFIER(parameter)
5657
IDENTIFIER(Protocol)

include/swift/Basic/SourceLoc.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class SourceRange {
9696
bool isValid() const { return Start.isValid(); }
9797
bool isInvalid() const { return !isValid(); }
9898

99+
bool operator==(const SourceRange &other) const {
100+
return Start == other.Start && End == other.End;
101+
}
102+
bool operator!=(const SourceRange &other) const { return !operator==(other); }
103+
99104
/// Print out the SourceRange. If the locations are in the same buffer
100105
/// as specified by LastBufferID, then we don't print the filename. If not,
101106
/// we do print the filename, and then update LastBufferID with the BufferID
@@ -134,6 +139,13 @@ class CharSourceRange {
134139
bool isValid() const { return Start.isValid(); }
135140
bool isInvalid() const { return !isValid(); }
136141

142+
bool operator==(const CharSourceRange &other) const {
143+
return Start == other.Start && ByteLength == other.ByteLength;
144+
}
145+
bool operator!=(const CharSourceRange &other) const {
146+
return !operator==(other);
147+
}
148+
137149
SourceLoc getStart() const { return Start; }
138150
SourceLoc getEnd() const { return Start.getAdvancedLocOrInvalid(ByteLength); }
139151

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3988,9 +3988,9 @@ ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
39883988

39893989
// Find the type we bridge to.
39903990
return ProtocolConformance::getTypeWitnessByName(type,
3991-
conformance->getConcrete(),
3992-
getIdentifier("_ObjectiveCType"),
3993-
resolver);
3991+
conformance->getConcrete(),
3992+
Id_ObjectiveCType,
3993+
resolver);
39943994
}
39953995

39963996
std::pair<ArchetypeBuilder *, ArchetypeBuilder::PotentialArchetype *>

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,14 +2769,6 @@ struct ASTNodeBase {};
27692769
}
27702770
checkSourceRanges(D->getSourceRange(), Parent,
27712771
[&]{ D->print(Out); });
2772-
if (auto *VD = dyn_cast<VarDecl>(D)) {
2773-
if (!VD->getTypeSourceRangeForDiagnostics().isValid()) {
2774-
Out << "invalid type source range for variable decl: ";
2775-
D->print(Out);
2776-
Out << "\n";
2777-
abort();
2778-
}
2779-
}
27802772
}
27812773

27822774
/// \brief Verify that the given source ranges is contained within the

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,13 +3369,14 @@ SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
33693369

33703370
Pattern *Pat = getParentPattern();
33713371
if (!Pat || Pat->isImplicit())
3372-
return getSourceRange();
3372+
return SourceRange();
33733373

33743374
if (auto *VP = dyn_cast<VarPattern>(Pat))
33753375
Pat = VP->getSubPattern();
33763376
if (auto *TP = dyn_cast<TypedPattern>(Pat))
33773377
return TP->getTypeLoc().getTypeRepr()->getSourceRange();
3378-
return getSourceRange();
3378+
3379+
return SourceRange();
33793380
}
33803381

33813382
static bool isVarInPattern(const VarDecl *VD, Pattern *P) {

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
807807
Type objcType = ProtocolConformance::getTypeWitnessByName(
808808
nominal->getDeclaredType(),
809809
conformance,
810-
ctx.getIdentifier("_ObjectiveCType"),
810+
ctx.Id_ObjectiveCType,
811811
nullptr);
812812
if (!objcType) return false;
813813

lib/SIL/Bridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
216216
assert(conformance && "Missing conformance?");
217217
Type bridgedTy =
218218
ProtocolConformance::getTypeWitnessByName(
219-
t, conformance, M.getASTContext().getIdentifier("_ObjectiveCType"),
219+
t, conformance, M.getASTContext().Id_ObjectiveCType,
220220
nullptr);
221221
assert(bridgedTy && "Missing _ObjectiveCType witness?");
222222
if (bridgedCollectionsAreOptional && clangTy)

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ SILGenModule::getBridgedObjectiveCTypeRequirement(SILLocation loc) {
241241
// Look for _bridgeToObjectiveC().
242242
auto &ctx = getASTContext();
243243
AssociatedTypeDecl *found = nullptr;
244-
DeclName name(ctx.getIdentifier("_ObjectiveCType"));
244+
DeclName name(ctx.Id_ObjectiveCType);
245245
for (auto member : proto->lookupDirect(name, true)) {
246246
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member)) {
247247
found = assocType;

0 commit comments

Comments
 (0)