@@ -369,7 +369,7 @@ static void GenerateXamlCodeBehind(XamlProjectItem? xamlItem, Compilation compil
369369 }
370370
371371 var uid = Crc64 . ComputeHashString ( $ "{ compilation . AssemblyName } .{ itemName } ") ;
372- if ( ! TryParseXaml ( xamlItem , uid , compilation , xmlnsCache , typeCache , context . CancellationToken , out var accessModifier , out var rootType , out var rootClrNamespace , out var generateDefaultCtor , out var addXamlCompilationAttribute , out var hideFromIntellisense , out var XamlResourceIdOnly , out var baseType , out var namedFields ) )
372+ if ( ! TryParseXaml ( xamlItem , uid , compilation , xmlnsCache , typeCache , context . CancellationToken , context . ReportDiagnostic , out var accessModifier , out var rootType , out var rootClrNamespace , out var generateDefaultCtor , out var addXamlCompilationAttribute , out var hideFromIntellisense , out var XamlResourceIdOnly , out var baseType , out var namedFields ) )
373373 {
374374 return ;
375375 }
@@ -472,7 +472,7 @@ static void GenerateXamlCodeBehind(XamlProjectItem? xamlItem, Compilation compil
472472 context . AddSource ( hintName , SourceText . From ( sb . ToString ( ) , Encoding . UTF8 ) ) ;
473473 }
474474
475- static bool TryParseXaml ( XamlProjectItem parseResult , string uid , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache , CancellationToken cancellationToken , out string ? accessModifier , out string ? rootType , out string ? rootClrNamespace , out bool generateDefaultCtor , out bool addXamlCompilationAttribute , out bool hideFromIntellisense , out bool xamlResourceIdOnly , out string ? baseType , out IEnumerable < ( string , string ? , string ) > ? namedFields )
475+ static bool TryParseXaml ( XamlProjectItem parseResult , string uid , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache , CancellationToken cancellationToken , Action < Diagnostic > reportDiagnostic , out string ? accessModifier , out string ? rootType , out string ? rootClrNamespace , out bool generateDefaultCtor , out bool addXamlCompilationAttribute , out bool hideFromIntellisense , out bool xamlResourceIdOnly , out string ? baseType , out IEnumerable < ( string , string ? , string ) > ? namedFields )
476476 {
477477 accessModifier = null ;
478478 rootType = null ;
@@ -522,9 +522,9 @@ static bool TryParseXaml(XamlProjectItem parseResult, string uid, Compilation co
522522 return true ;
523523 }
524524
525- namedFields = GetNamedFields ( root , nsmgr , compilation , xmlnsCache , typeCache , cancellationToken ) ;
525+ namedFields = GetNamedFields ( root , nsmgr , compilation , xmlnsCache , typeCache , cancellationToken , reportDiagnostic ) ;
526526 var typeArguments = GetAttributeValue ( root , "TypeArguments" , XamlParser . X2006Uri , XamlParser . X2009Uri ) ;
527- baseType = GetTypeName ( new XmlType ( root . NamespaceURI , root . LocalName , typeArguments != null ? TypeArgumentsParser . ParseExpression ( typeArguments , nsmgr , null ) : null ) , compilation , xmlnsCache , typeCache ) ;
527+ baseType = GetTypeName ( new XmlType ( root . NamespaceURI , root . LocalName , typeArguments != null ? TypeArgumentsParser . ParseExpression ( typeArguments , nsmgr , null ) : null ) , compilation , xmlnsCache , typeCache , reportDiagnostic ) ;
528528 if ( baseType == null )
529529 return false ;
530530
@@ -554,7 +554,7 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
554554 return true ;
555555 }
556556
557- static IEnumerable < ( string name , string ? type , string accessModifier ) > GetNamedFields ( XmlNode root , XmlNamespaceManager nsmgr , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache , CancellationToken cancellationToken )
557+ static IEnumerable < ( string name , string ? type , string accessModifier ) > GetNamedFields ( XmlNode root , XmlNamespaceManager nsmgr , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache , CancellationToken cancellationToken , Action < Diagnostic > reportDiagnostic )
558558 {
559559 var xPrefix = nsmgr . LookupPrefix ( XamlParser . X2006Uri ) ?? nsmgr . LookupPrefix ( XamlParser . X2009Uri ) ;
560560 if ( xPrefix == null )
@@ -586,11 +586,11 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
586586 accessModifier = "private" ;
587587 }
588588
589- yield return ( name ?? "" , GetTypeName ( xmlType , compilation , xmlnsCache , typeCache ) , accessModifier ) ;
589+ yield return ( name ?? "" , GetTypeName ( xmlType , compilation , xmlnsCache , typeCache , reportDiagnostic ) , accessModifier ) ;
590590 }
591591 }
592592
593- static string ? GetTypeName ( XmlType xmlType , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache )
593+ static string ? GetTypeName ( XmlType xmlType , Compilation compilation , AssemblyCaches xmlnsCache , IDictionary < XmlType , string > typeCache , Action < Diagnostic > reportDiagnostic )
594594 {
595595 if ( typeCache . TryGetValue ( xmlType , out string returnType ) )
596596 {
@@ -605,12 +605,12 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
605605 else
606606 {
607607 // It's an external, non-built-in namespace URL.
608- returnType = GetTypeNameFromCustomNamespace ( xmlType , compilation , xmlnsCache ) ;
608+ returnType = GetTypeNameFromCustomNamespace ( xmlType , compilation , xmlnsCache , reportDiagnostic ) ;
609609 }
610610
611611 if ( xmlType . TypeArguments != null )
612612 {
613- returnType = $ "{ returnType } <{ string . Join ( ", " , xmlType . TypeArguments . Select ( typeArg => GetTypeName ( typeArg , compilation , xmlnsCache , typeCache ) ) ) } >";
613+ returnType = $ "{ returnType } <{ string . Join ( ", " , xmlType . TypeArguments . Select ( typeArg => GetTypeName ( typeArg , compilation , xmlnsCache , typeCache , reportDiagnostic ) ) ) } >";
614614 }
615615
616616 if ( returnType == null )
@@ -640,10 +640,10 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
640640 return XmlnsHelper . ParseNamespaceFromXmlns ( namespaceuri ) ;
641641 }
642642
643- static string GetTypeNameFromCustomNamespace ( XmlType xmlType , Compilation compilation , AssemblyCaches xmlnsCache )
643+ static string GetTypeNameFromCustomNamespace ( XmlType xmlType , Compilation compilation , AssemblyCaches xmlnsCache , Action < Diagnostic > reportDiagnostic )
644644 {
645645#nullable disable
646- string typeName = xmlType . GetTypeReference < string > ( xmlnsCache . XmlnsDefinitions , null ,
646+ IEnumerable < string > typeNames = xmlType . GetTypeReferences < string > ( xmlnsCache . XmlnsDefinitions , null ,
647647 ( typeInfo ) =>
648648 {
649649 string typeName = typeInfo . typeName . Replace ( '+' , '/' ) ; //Nested types
@@ -679,7 +679,13 @@ static string GetTypeNameFromCustomNamespace(XmlType xmlType, Compilation compil
679679 return null ;
680680 } ) ;
681681
682- return typeName ;
682+ if ( typeNames . Distinct ( ) . Skip ( 1 ) . Any ( ) )
683+ {
684+ reportDiagnostic ( Diagnostic . Create ( Descriptors . AmbiguousType , Location . None ,
685+ new [ ] { xmlType . Name , xmlType . NamespaceUri } . ToArray ( ) ) ) ;
686+
687+ }
688+ return typeNames . FirstOrDefault ( ) ;
683689#nullable enable
684690 }
685691
@@ -893,9 +899,6 @@ public bool Equals(Compilation x, Compilation y)
893899 return x . ExternalReferences . OfType < PortableExecutableReference > ( ) . SequenceEqual ( y . ExternalReferences . OfType < PortableExecutableReference > ( ) ) ;
894900 }
895901
896- public int GetHashCode ( Compilation obj )
897- {
898- return obj . References . GetHashCode ( ) ;
899- }
902+ public int GetHashCode ( Compilation obj ) => obj . References . GetHashCode ( ) ;
900903 }
901904}
0 commit comments