Skip to content

Commit 1d7d282

Browse files
Merge pull request #35255 from LucianoPAlmeida/SR-12383
[SR-12383][Diagnostics] Do not use canonical type for aka diagnostics instead walk the type recursivelly desugaring
2 parents 5938a67 + b9dec7d commit 1d7d282

File tree

8 files changed

+38
-13
lines changed

8 files changed

+38
-13
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,6 @@ static bool shouldShowAKA(Type type, StringRef typeName) {
424424
if (type->isCanonical())
425425
return false;
426426

427-
// Don't show generic type parameters.
428-
if (type->getCanonicalType()->hasTypeParameter())
429-
return false;
430-
431427
// Only show 'aka' if there's a typealias involved; other kinds of sugar
432428
// are easy enough for people to read on their own.
433429
if (!type.findIf(isInterestingTypealias))
@@ -459,6 +455,19 @@ static bool typeSpellingIsAmbiguous(Type type,
459455
return false;
460456
}
461457

458+
/// Walks the type recursivelly desugaring types to display, but skipping
459+
/// `GenericTypeParamType` because we would lose association with its original
460+
/// declaration and end up presenting the parameter in τ_0_0 format on
461+
/// diagnostic.
462+
static Type getAkaTypeForDisplay(Type type) {
463+
return type.transform([](Type visitTy) -> Type {
464+
if (isa<SugarType>(visitTy.getPointer()) &&
465+
!isa<GenericTypeParamType>(visitTy.getPointer()))
466+
return getAkaTypeForDisplay(visitTy->getDesugaredType());
467+
return visitTy;
468+
});
469+
}
470+
462471
/// Format a single diagnostic argument and write it to the given
463472
/// stream.
464473
static void formatDiagnosticArgument(StringRef Modifier,
@@ -577,7 +586,8 @@ static void formatDiagnosticArgument(StringRef Modifier,
577586
if (shouldShowAKA(type, typeName)) {
578587
llvm::SmallString<256> AkaText;
579588
llvm::raw_svector_ostream OutAka(AkaText);
580-
OutAka << type->getCanonicalType();
589+
590+
OutAka << getAkaTypeForDisplay(type);
581591
Out << llvm::format(FormatOpts.AKAFormatString.c_str(),
582592
typeName.c_str(), AkaText.c_str());
583593
} else {

test/AutoDiff/Sema/differentiable_attr_type_checking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ struct TF_521<T: FloatingPoint> {
392392
}
393393
// expected-error @+1 {{type 'TF_521<T>' does not conform to protocol 'Differentiable'}}
394394
extension TF_521: Differentiable where T: Differentiable {
395-
// expected-note @+1 {{possibly intended match 'TF_521<T>.TangentVector' does not conform to 'AdditiveArithmetic'}}
395+
// expected-note @+1 {{possibly intended match 'TF_521<T>.TangentVector' (aka 'TF_521<T>') does not conform to 'AdditiveArithmetic'}}
396396
typealias TangentVector = TF_521
397397
}
398398
// expected-error @+1 {{result type 'TF_521<Float>' does not conform to 'Differentiable', but the enclosing function type is '@differentiable'}}

test/ClangImporter/objc_generics_conformance.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protocol WithAssocOther {
2121
}
2222

2323
extension GenericClass : WithAssocOther {
24-
typealias Other = [T] // expected-error{{type 'GenericClass<T>.Other' involving Objective-C type parameter 'T' cannot be used for associated type 'Other' of protocol 'WithAssocOther'}}
24+
typealias Other = [T] // expected-error{{type 'GenericClass<T>.Other' (aka 'Array<T>') involving Objective-C type parameter 'T' cannot be used for associated type 'Other' of protocol 'WithAssocOther'}}
2525
}
2626

2727
protocol WithAssocSeparate {
@@ -31,7 +31,7 @@ protocol WithAssocSeparate {
3131
extension GenericClass {
3232
typealias Separate = T // expected-note {{'Separate' declared here}}
3333
}
34-
extension GenericClass : WithAssocSeparate { // expected-error {{type 'GenericClass<T>.Separate' involving Objective-C type parameter 'T' cannot be used for associated type 'Separate' of protocol 'WithAssocSeparate'}}
34+
extension GenericClass : WithAssocSeparate { // expected-error {{type 'GenericClass<T>.Separate' (aka 'T') involving Objective-C type parameter 'T' cannot be used for associated type 'Separate' of protocol 'WithAssocSeparate'}}
3535
}
3636

3737
protocol WithAssocElement {

test/Constraints/conditionally_defined_types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let _ = SameType<X>.Decl3.self
3636
let _ = SameType<X>.Decl4<X>.self
3737
let _ = SameType<X>.Decl5<X>.self
3838

39-
let _ = SameType<Y>.TypeAlias1.self // expected-error {{'SameType<T>.TypeAlias1' requires the types 'Y' and 'X' be equivalent}}
39+
let _ = SameType<Y>.TypeAlias1.self // expected-error {{'SameType<T>.TypeAlias1' (aka 'T') requires the types 'Y' and 'X' be equivalent}}
4040
let _ = SameType<Y>.TypeAlias2.self // expected-error {{'SameType<T>.TypeAlias2' (aka 'Y') requires the types 'Y' and 'X' be equivalent}}
4141
let _ = SameType<Y>.TypeAlias3<X>.self // expected-error {{'SameType<Y>.TypeAlias3' requires the types 'Y' and 'X' be equivalent}}
4242
let _ = SameType<Y>.Decl1.self // expected-error {{'SameType<T>.Decl1' requires the types 'Y' and 'X' be equivalent}}
@@ -144,7 +144,7 @@ let _ = SameType<X>.Decl4<X>.Decl3.self
144144
let _ = SameType<X>.Decl4<X>.Decl4<X>.self
145145
let _ = SameType<X>.Decl4<X>.Decl5<X>.self
146146

147-
let _ = SameType<X>.Decl4<Y>.TypeAlias1.self // expected-error {{'SameType<T>.Decl4<U>.TypeAlias1' requires the types 'Y' and 'X' be equivalent}}
147+
let _ = SameType<X>.Decl4<Y>.TypeAlias1.self // expected-error {{'SameType<T>.Decl4<U>.TypeAlias1' (aka 'T') requires the types 'Y' and 'X' be equivalent}}
148148
let _ = SameType<X>.Decl4<Y>.TypeAlias2.self // expected-error {{'SameType<T>.Decl4<U>.TypeAlias2' (aka 'Y') requires the types 'Y' and 'X' be equivalent}}
149149
let _ = SameType<X>.Decl4<Y>.TypeAlias3<X>.self // expected-error {{'SameType<X>.Decl4<Y>.TypeAlias3' requires the types 'Y' and 'X' be equivalent}}
150150
let _ = SameType<X>.Decl4<Y>.Decl1.self // expected-error {{'SameType<T>.Decl4<U>.Decl1' requires the types 'Y' and 'X' be equivalent}}

test/Constraints/sr12383.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol Protocol {
4+
associatedtype Index: Comparable
5+
subscript(bounds: Range<Index>) -> Int { get }
6+
// expected-note@+1 {{protocol requires subscript with type '(Wrapper<Base>.Index) -> Int' (aka '(Base.Index) -> Int'); do you want to add a stub?}}
7+
subscript(position: Index) -> Int { get }
8+
}
9+
10+
struct Wrapper<Base: Protocol>: Protocol { // expected-error {{type 'Wrapper<Base>' does not conform to protocol 'Protocol'}}
11+
typealias Index = Base.Index
12+
subscript(bounds: Range<Index>) -> Int { // expected-note {{candidate has non-matching type '<Base> (Range<Wrapper<Base>.Index>) -> Int' (aka '<Base> (Range<Base.Index>) -> Int')}}
13+
get { 1 }
14+
}
15+
}

test/Generics/protocol_type_aliases.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func requirementOnConcreteNestedTypeAlias<T>(_: T) where T: Q2, T.C == T.B.X {}
3535
// CHECK-LABEL: .concreteRequirementOnConcreteNestedTypeAlias@
3636
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Q2, τ_0_0.C == τ_0_0.B.A>
3737
func concreteRequirementOnConcreteNestedTypeAlias<T>(_: T) where T: Q2, S<T.C> == T.B.X {}
38-
// expected-warning@-1 {{neither type in same-type constraint ('S<T.C>' or 'T.B.X') refers to a generic parameter or associated type}}
38+
// expected-warning@-1 {{neither type in same-type constraint ('S<T.C>' or 'T.B.X' (aka 'S<T.B.A>')) refers to a generic parameter or associated type}}
3939

4040

4141
// Incompatible concrete typealias types are flagged as such

test/ImportResolution/import-specific-fixits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import struct DeclsUsedWrongly.Choice // expected-error {{'Choice' was imported
3636
import struct DeclsUsedWrongly.Callback // expected-error {{type alias 'Callback' (aka '() -> ()') cannot be imported as 'struct'}} {{8-14=typealias}}
3737
import var DeclsUsedWrongly.Callback // expected-error {{'Callback' was imported as 'var', but is a type}} {{8-11=typealias}}
3838

39-
import struct DeclsUsedWrongly.Pair // expected-error {{type alias 'Pair' cannot be imported as 'struct'}} {{8-14=typealias}}
39+
import struct DeclsUsedWrongly.Pair // expected-error {{type alias 'Pair' (aka '(T, T)') cannot be imported as 'struct'}} {{8-14=typealias}}
4040
import var DeclsUsedWrongly.Pair // expected-error {{'Pair' was imported as 'var', but is a type}} {{8-11=typealias}}
4141

4242
import struct Swift.print // expected-error {{'print' was imported as 'struct', but is a function}} {{8-14=func}}

validation-test/compiler_crashers_2_fixed/sr12990.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ContainerTransition {
55
func completeTransition() {
66
viewControllers?[Int//.max
77
// expected-error@-1 {{no exact matches in call to subscript}}
8-
// expected-note@-2 {{found candidate with type '((Int).Type) -> Dictionary<Int, String>.SubSequence' (aka '(Int.Type) -> Slice<Dictionary<Int, String>>')}}
8+
// expected-note@-2 {{found candidate with type '((Int).Type) -> Dictionary<Int, String>.SubSequence' (aka '((Int).Type) -> Slice<Dictionary<Int, String>>')}}
99
// expected-note@-3 {{to match this opening '['}}
1010
} // expected-error {{expected ']' in expression list}}
1111
}

0 commit comments

Comments
 (0)