@@ -2434,6 +2434,53 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
24342434 }
24352435 }
24362436
2437+ // If we have more than one attribute declaration, we have an ambiguity.
2438+ // So, emit an ambiguity diagnostic.
2439+ if (auto typeRepr = attr->getTypeRepr ()) {
2440+ if (nominals.size () > 1 ) {
2441+ SmallVector<NominalTypeDecl *, 4 > ambiguousCandidates;
2442+ // Filter out declarations that cannot be attributes.
2443+ for (auto decl : nominals) {
2444+ if (isa<ProtocolDecl>(decl)) {
2445+ continue ;
2446+ }
2447+ ambiguousCandidates.push_back (decl);
2448+ }
2449+ if (ambiguousCandidates.size () > 1 ) {
2450+ auto attrName = nominals.front ()->getName ();
2451+ ctx.Diags .diagnose (typeRepr->getLoc (),
2452+ diag::ambiguous_custom_attribute_ref, attrName);
2453+ for (auto candidate : ambiguousCandidates) {
2454+ ctx.Diags .diagnose (candidate->getLoc (),
2455+ diag::found_attribute_candidate);
2456+ // If the candidate is a top-level attribute, let's suggest
2457+ // adding module name to resolve the ambiguity.
2458+ if (candidate->getDeclContext ()->isModuleScopeContext ()) {
2459+ auto moduleName = candidate->getParentModule ()->getName ();
2460+ ctx.Diags
2461+ .diagnose (typeRepr->getLoc (),
2462+ diag::ambiguous_custom_attribute_ref_fix,
2463+ moduleName.str (), attrName, moduleName)
2464+ .fixItInsert (typeRepr->getLoc (), moduleName.str ().str () + " ." );
2465+ }
2466+ }
2467+ return nullptr ;
2468+ }
2469+ }
2470+ }
2471+
2472+ // There is no nominal type with this name, so complain about this being
2473+ // an unknown attribute.
2474+ std::string typeName;
2475+ if (auto typeRepr = attr->getTypeRepr ()) {
2476+ llvm::raw_string_ostream out (typeName);
2477+ typeRepr->print (out);
2478+ } else {
2479+ typeName = attr->getType ().getString ();
2480+ }
2481+
2482+ ctx.Diags .diagnose (attr->getLocation (), diag::unknown_attribute, typeName);
2483+
24372484 return nullptr ;
24382485}
24392486
0 commit comments