@@ -483,7 +483,7 @@ private void GenerateVariableInfoMap(
483
483
if ( symbol is IParameterSymbol && variableDeclared )
484
484
continue ;
485
485
486
- var type = GetSymbolType ( symbol ) ;
486
+ var type = GetSymbolTypeWithUpdatedNullability ( symbol ) ;
487
487
if ( type == null )
488
488
continue ;
489
489
@@ -527,16 +527,9 @@ PooledDisposer<PooledHashSet<ISymbol>> GetPooledSymbolSet(ImmutableArray<ISymbol
527
527
return disposer ;
528
528
}
529
529
530
- ITypeSymbol ? GetSymbolType ( ISymbol symbol )
530
+ ITypeSymbol ? GetSymbolTypeWithUpdatedNullability ( ISymbol symbol )
531
531
{
532
- var type = symbol switch
533
- {
534
- ILocalSymbol local => local . Type ,
535
- IParameterSymbol parameter => parameter . Type ,
536
- IRangeVariableSymbol rangeVariable => GetRangeVariableType ( rangeVariable ) ,
537
- _ => throw ExceptionUtilities . UnexpectedValue ( symbol )
538
- } ;
539
-
532
+ var type = GetUnderlyingSymbolType ( symbol ) ;
540
533
if ( type is null )
541
534
return type ;
542
535
@@ -578,6 +571,17 @@ static VariableInfo CreateFromSymbol(
578
571
}
579
572
}
580
573
574
+ private ITypeSymbol ? GetUnderlyingSymbolType ( ISymbol symbol )
575
+ {
576
+ return symbol switch
577
+ {
578
+ ILocalSymbol local => local . Type ,
579
+ IParameterSymbol parameter => parameter . Type ,
580
+ IRangeVariableSymbol rangeVariable => GetRangeVariableType ( rangeVariable ) ,
581
+ _ => throw ExceptionUtilities . UnexpectedValue ( symbol )
582
+ } ;
583
+ }
584
+
581
585
private static void AddVariableToMap ( IDictionary < ISymbol , VariableInfo > variableInfoMap , ISymbol localOrParameter , VariableInfo variableInfo )
582
586
=> variableInfoMap . Add ( localOrParameter , variableInfo ) ;
583
587
@@ -715,15 +719,13 @@ private static bool IsInteractiveSynthesizedParameter(ISymbol localOrParameter)
715
719
parameter . ContainingSymbol . ContainingType . IsScriptClass ;
716
720
}
717
721
718
- private static void AddTypeParametersToMap ( IEnumerable < ITypeParameterSymbol > typeParameters , IDictionary < int , ITypeParameterSymbol > sortedMap )
722
+ private void AddTypeParametersToMap ( IEnumerable < ITypeParameterSymbol > typeParameters , IDictionary < int , ITypeParameterSymbol > sortedMap )
719
723
{
720
724
foreach ( var typeParameter in typeParameters )
721
- {
722
725
AddTypeParameterToMap ( typeParameter , sortedMap ) ;
723
- }
724
726
}
725
727
726
- private static void AddTypeParameterToMap ( ITypeParameterSymbol typeParameter , IDictionary < int , ITypeParameterSymbol > sortedMap )
728
+ private void AddTypeParameterToMap ( ITypeParameterSymbol typeParameter , IDictionary < int , ITypeParameterSymbol > sortedMap )
727
729
{
728
730
if ( typeParameter == null ||
729
731
typeParameter . DeclaringMethod == null ||
@@ -732,6 +734,13 @@ private static void AddTypeParameterToMap(ITypeParameterSymbol typeParameter, ID
732
734
return ;
733
735
}
734
736
737
+ // Only care about type parameters declared outside of the span being selected. If the type parameter
738
+ // is within the selection, that means it comes from a generic local function and would not otherwise be
739
+ // usable by the calling method.
740
+ var selectionSpan = this . SelectionResult . FinalSpan ;
741
+ if ( typeParameter . Locations is not [ var location ] || selectionSpan . Contains ( location . SourceSpan ) )
742
+ return ;
743
+
735
744
sortedMap [ typeParameter . Ordinal ] = typeParameter ;
736
745
}
737
746
@@ -740,29 +749,10 @@ private void AppendMethodTypeVariableFromDataFlowAnalysis(
740
749
IDictionary < int , ITypeParameterSymbol > sortedMap )
741
750
{
742
751
foreach ( var symbol in variableInfoMap . Keys )
743
- {
744
- switch ( symbol )
745
- {
746
- case IParameterSymbol parameter :
747
- AddTypeParametersToMap ( TypeParameterCollector . Collect ( parameter . Type ) , sortedMap ) ;
748
- continue ;
749
-
750
- case ILocalSymbol local :
751
- AddTypeParametersToMap ( TypeParameterCollector . Collect ( local . Type ) , sortedMap ) ;
752
- continue ;
753
-
754
- case IRangeVariableSymbol rangeVariable :
755
- var type = GetRangeVariableType ( rangeVariable ) ;
756
- AddTypeParametersToMap ( TypeParameterCollector . Collect ( type ) , sortedMap ) ;
757
- continue ;
758
-
759
- default :
760
- throw ExceptionUtilities . UnexpectedValue ( symbol ) ;
761
- }
762
- }
752
+ AddTypeParametersToMap ( TypeParameterCollector . Collect ( GetUnderlyingSymbolType ( symbol ) ) , sortedMap ) ;
763
753
}
764
754
765
- private static void AppendMethodTypeParameterFromConstraint ( SortedDictionary < int , ITypeParameterSymbol > sortedMap )
755
+ private void AppendMethodTypeParameterFromConstraint ( SortedDictionary < int , ITypeParameterSymbol > sortedMap )
766
756
{
767
757
var typeParametersInConstraint = new List < ITypeParameterSymbol > ( ) ;
768
758
@@ -771,9 +761,7 @@ private static void AppendMethodTypeParameterFromConstraint(SortedDictionary<int
771
761
{
772
762
var constraintTypes = typeParameter . ConstraintTypes ;
773
763
if ( constraintTypes . IsDefaultOrEmpty )
774
- {
775
764
continue ;
776
- }
777
765
778
766
foreach ( var type in constraintTypes )
779
767
{
@@ -784,21 +772,13 @@ private static void AppendMethodTypeParameterFromConstraint(SortedDictionary<int
784
772
785
773
// pick up only valid type parameter and add them to the map
786
774
foreach ( var typeParameter in typeParametersInConstraint )
787
- {
788
775
AddTypeParameterToMap ( typeParameter , sortedMap ) ;
789
- }
790
776
}
791
777
792
- private static void AppendMethodTypeParameterUsedDirectly ( MultiDictionary < ISymbol , SyntaxToken > symbolMap , IDictionary < int , ITypeParameterSymbol > sortedMap )
778
+ private void AppendMethodTypeParameterUsedDirectly ( MultiDictionary < ISymbol , SyntaxToken > symbolMap , IDictionary < int , ITypeParameterSymbol > sortedMap )
793
779
{
794
780
foreach ( var typeParameter in symbolMap . Keys . OfType < ITypeParameterSymbol > ( ) )
795
- {
796
- if ( typeParameter . DeclaringMethod != null &&
797
- ! sortedMap . ContainsKey ( typeParameter . Ordinal ) )
798
- {
799
- sortedMap [ typeParameter . Ordinal ] = typeParameter ;
800
- }
801
- }
781
+ AddTypeParameterToMap ( typeParameter , sortedMap ) ;
802
782
}
803
783
804
784
private ImmutableArray < ITypeParameterSymbol > GetMethodTypeParametersInConstraintList (
@@ -816,7 +796,7 @@ private ImmutableArray<ITypeParameterSymbol> GetMethodTypeParametersInConstraint
816
796
return [ .. sortedMap . Values ] ;
817
797
}
818
798
819
- private static void AppendTypeParametersInConstraintsUsedByConstructedTypeWithItsOwnConstraints ( SortedDictionary < int , ITypeParameterSymbol > sortedMap )
799
+ private void AppendTypeParametersInConstraintsUsedByConstructedTypeWithItsOwnConstraints ( SortedDictionary < int , ITypeParameterSymbol > sortedMap )
820
800
{
821
801
using var _1 = PooledHashSet < ITypeSymbol > . GetInstance ( out var visited ) ;
822
802
using var _2 = PooledHashSet < ITypeParameterSymbol > . GetInstance ( out var candidates ) ;
@@ -877,7 +857,7 @@ private static void AddTypeParametersInConstraintsUsedByConstructedTypeWithItsOw
877
857
}
878
858
}
879
859
880
- private static ImmutableArray < ITypeParameterSymbol > GetMethodTypeParametersInDeclaration ( ITypeSymbol returnType , SortedDictionary < int , ITypeParameterSymbol > sortedMap )
860
+ private ImmutableArray < ITypeParameterSymbol > GetMethodTypeParametersInDeclaration ( ITypeSymbol returnType , SortedDictionary < int , ITypeParameterSymbol > sortedMap )
881
861
{
882
862
// add return type to the map
883
863
AddTypeParametersToMap ( TypeParameterCollector . Collect ( returnType ) , sortedMap ) ;
0 commit comments