Cannot get Symbol for Alias part of UsingSyntax

Answered Cannot get Symbol for Alias part of UsingSyntax

  • Saturday, February 11, 2012 7:10 AM
     
      Has Code

    I expected the following unit test to return a Symbol for System.Object from the alias foo I created. I actually get null. Am I not passing the correct SyntaxNode into GetDeclaredSymbol?

            [TestMethod]
            public void UsingTest() {
                var compilation = Compilation.Create("foo.dll",
                    references: new[] { 
                        MetadataReference.Create(typeof(object).Assembly.Location),
                    },
                    syntaxTrees: new[] { 
                        SyntaxTree.ParseCompilationUnit(
                            "using foo = System.Object;"
                        ) 
                    }
                );
    
                var tree = compilation.SyntaxTrees.Single();
                var cu = (CompilationUnitSyntax)tree.Root;
                var usingSyntax = cu.Usings.Single();
                var alias = usingSyntax.AliasOpt;
    
                var semanticModel = compilation.GetSemanticModel(tree);
                var info = semanticModel.GetDeclaredSymbol(alias);
    
                Assert.IsNotNull(info);
            }
    

    Thanks,
    Chris

All Replies

  • Saturday, February 11, 2012 2:38 PM
     
      Has Code

    If you look at the overloads of GetDeclaredSymbol(), you'll notice there is one for UsingDirectiveSyntax. So I think you need to pass in the whole UsingDirectiveSyntax. The following code works for me:

    var info = semanticModel.GetDeclaredSymbol(usingSyntax);

    On the other hand, the documentation for the overload that you're using says:

    A syntax node that is a declaration. This can be any type derived from MemberDeclarationSyntax, TypeDeclarationSyntax, EnumDeclarationSyntax, NamespaceDeclarationSyntax, ParameterSyntax, TypeParameterSyntax, or the alias part of a UsingDirectiveSyntax.

    I think you are trying to use “the alias part of a UsingDirectiveSyntax”, so this might be a bug.

    • Edited by svick Saturday, February 11, 2012 2:45 PM
    •  
  • Saturday, February 11, 2012 8:35 PM
     
     

    Svick, yeah, the bug might be in the documentation. Although I did expect for the alias portion and the identifier portion to be resolveable to a symbol. 

    Thanks,
    Chris

  • Monday, February 13, 2012 9:01 PM
    Owner
     
     Answered

    svick is correct - GetDeclaredSymbol() expects a UsingDirectiveSyntax instead of just the alias part. The doc comment text looks like a bug to me.

    When you pass just the alias part (i.e. 'NameEqualsSyntax') of the using directive, this results in calling a generic overload of GetDeclaredSymbol() (i.e. an overload that accepts any 'SyntaxNode'). On the other hand, when you pass the using directive this results in calling a more specific overload of GetDeclaredSymbol (i.e. an overload that accepts exactly a 'UsingDirectiveSyntax')...


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

    • Marked As Answer by kingces95 Tuesday, February 14, 2012 9:13 PM
    •  
  • Monday, February 13, 2012 9:39 PM
    Owner
     
     

    Also, in our recent design thinking, we are considering removing the generic overload of GetDeclaredSymbol() i.e. the one that accepts any 'SyntaxNode'. This would mean that we would only be able to call GetDeclaredSymbols() with syntax types that 'can' map to declared symbols. This should help make the API less ambiguous.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team