@@ -2862,105 +2862,9 @@ namespace {
2862
2862
if (!result)
2863
2863
return nullptr ;
2864
2864
2865
- // Check for ambiguous member if the base is an Optional
2866
- if (baseTy->getOptionalObjectType ()) {
2867
- diagnoseAmbiguousNominalMember (baseTy, result);
2868
- }
2869
-
2870
2865
return coerceToType (result, resultTy, cs.getConstraintLocator (expr));
2871
2866
}
2872
-
2873
- // / Diagnose if the base type is optional, we're referring to a nominal
2874
- // / type member via the dot syntax and the member name matches
2875
- // / Optional<T>.{member name}
2876
- void diagnoseAmbiguousNominalMember (Type baseTy, Expr *result) {
2877
- if (auto baseTyUnwrapped = baseTy->lookThroughAllOptionalTypes ()) {
2878
- // Return if the base type doesn't have a nominal type decl
2879
- if (!baseTyUnwrapped->getNominalOrBoundGenericNominal ()) {
2880
- return ;
2881
- }
2882
-
2883
- // Otherwise, continue digging
2884
- if (auto DSCE = dyn_cast<DotSyntaxCallExpr>(result)) {
2885
- auto calledValue = DSCE->getCalledValue ();
2886
- auto isOptional = false ;
2887
- Identifier memberName;
2888
-
2889
- // Try cast the assigned value to an enum case
2890
- //
2891
- // This will always succeed if the base is Optional<T> & the
2892
- // assigned case comes from Optional<T>
2893
- if (auto EED = dyn_cast<EnumElementDecl>(calledValue)) {
2894
- isOptional = EED->getParentEnum ()->isOptionalDecl ();
2895
- memberName = EED->getBaseIdentifier ();
2896
- }
2897
-
2898
- // Return if the enum case doesn't come from Optional<T>
2899
- if (!isOptional) {
2900
- return ;
2901
- }
2902
-
2903
- // Look up the enum case in the unwrapped type to check if it exists
2904
- // as a member
2905
- auto baseTyNominalDecl = baseTyUnwrapped
2906
- ->getNominalOrBoundGenericNominal ();
2907
- auto results = TypeChecker::lookupMember (
2908
- baseTyNominalDecl->getModuleContext (), baseTyUnwrapped,
2909
- DeclNameRef (memberName), defaultMemberLookupOptions);
2910
-
2911
- // Filter out any functions, instance members, enum cases with
2912
- // associated values or variables whose type does not match the
2913
- // contextual type.
2914
- results.filter ([&](const LookupResultEntry entry, bool isOuter) {
2915
- if (auto member = entry.getValueDecl ()) {
2916
- if (isa<FuncDecl>(member))
2917
- return false ;
2918
- if (member->isInstanceMember ())
2919
- return false ;
2920
- if (auto EED = dyn_cast<EnumElementDecl>(member)) {
2921
- return !EED->hasAssociatedValues ();
2922
- }
2923
- if (auto VD = dyn_cast<VarDecl>(member)) {
2924
- auto baseType = DSCE->getType ()->lookThroughAllOptionalTypes ();
2925
- return VD->getInterfaceType ()->isEqual (baseType);
2926
- }
2927
- }
2928
-
2929
- // Filter out anything that's not one of the above. We don't care
2930
- // if we have a typealias named 'none' or a struct/class named
2931
- // 'none'.
2932
- return false ;
2933
- });
2934
-
2935
- if (results.empty ()) {
2936
- return ;
2937
- }
2938
2867
2939
- auto &de = cs.getASTContext ().Diags ;
2940
- if (auto member = results.front ().getValueDecl ()) {
2941
- // Emit a diagnostic with some fix-its
2942
- auto baseTyName = baseTy->getCanonicalType ().getString ();
2943
- auto baseTyUnwrappedName = baseTyUnwrapped->getString ();
2944
- auto loc = DSCE->getLoc ();
2945
- auto startLoc = DSCE->getStartLoc ();
2946
- de.diagnoseWithNotes (
2947
- de.diagnose (loc, swift::diag::optional_ambiguous_case_ref,
2948
- baseTyName, baseTyUnwrappedName, memberName.str ()),
2949
- [&]() {
2950
- de.diagnose (loc,
2951
- swift::diag::optional_fixit_ambiguous_case_ref)
2952
- .fixItInsert (startLoc, " Optional" );
2953
- de.diagnose (
2954
- loc,
2955
- swift::diag::type_fixit_optional_ambiguous_case_ref,
2956
- baseTyUnwrappedName, memberName.str ())
2957
- .fixItInsert (startLoc, baseTyUnwrappedName);
2958
- });
2959
- }
2960
- }
2961
- }
2962
- }
2963
-
2964
2868
private:
2965
2869
// / A list of "suspicious" optional injections that come from
2966
2870
// / forced downcasts.
0 commit comments