Coder Social home page Coder Social logo

Comments (7)

dgrunwald avatar dgrunwald commented on May 28, 2024

This is because the VariableInitializer resolves to the variable declaration, there's no node that resolves to the initialization assignment.
And ConversionsResolveResults only appear when an expression is used; if you resolve only "F" you won't get a ConversionResolveResult in either case.
To retrieve the conversion for the top-level expression you are resolving, use the astResolver.GetConversion()/GetExpectedType() methods.

from nrefactory.

erik-kallen avatar erik-kallen commented on May 28, 2024

I (think I) now understand why it works this way, but I can't help thinking that it's non-intuitive. After all, the expression means "Create a variable f with the value of the method group F converted to System.Action (or something), just like long l = 1 (which I guess has the same problem) means "Create a variable l with the value 1 converted to long."

If it is not feasible to make the method group in this case resolve to a converted method group, perhaps each variable initializer could resolve to a VariableInitializerResolveResult, like

public class VariableInitializerResolveResult {
    public IVariable Variable { get; }
    public ResolveResult Value { get; }
}

where the value would be appropriately converted?

from nrefactory.

erik-kallen avatar erik-kallen commented on May 28, 2024

Also, I guess that due to this, find references will not find any op_Implicit that is used during variable initialization.

from nrefactory.

dgrunwald avatar dgrunwald commented on May 28, 2024

Do you have a test case for that? Find References for op_Implicit in variable initializers works fine for me:

    [Test]
    public void FindReferencesForOpImplicitInLocalVariableInitialization()
    {
        Init(@"using System;
class Test {
 static void T() {
  int x = new Test();
 }
 public static implicit operator int(Test x) { return 0; }
}");
        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "Test");
        var opImplicit = test.Methods.Single(m => m.Name == "op_Implicit");
        var actual = FindReferences(opImplicit).ToList();
        Assert.AreEqual(2, actual.Count);
        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is ObjectCreateExpression));
        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 6 && r is OperatorDeclaration));
    }

FindReferences for op_Implicit isn't looking for ConversionResolveResults; it directly looks at all conversions found by the resolver.

from nrefactory.

erik-kallen avatar erik-kallen commented on May 28, 2024

Sorry, I just assumed that this would be the case, and assumptions are always bad. I still claim, though, that that the API would be more usable if there were something that resolved to the ConversionResult representing the method group conversion.

Btw, I have already made the adjustments in my code to compensate for this, and it works but it looks kind of ugly:

foreach (var d in variableDeclarationStatement.Variables) {
    var variable = ((LocalResolveResult)_resolver.Resolve(d)).Variable;
    if (!d.Initializer.IsNull) {
        var initializer = _resolver.Resolve(d.Initializer);
        if (!initializer.Type.Equals(variable.Type)) {
            initializer = new ConversionResolveResult(variable.Type, initializer, CSharpConversions.Get(_compilation).ImplicitConversion(initializer, variable.Type));
        }
        ...

from nrefactory.

dgrunwald avatar dgrunwald commented on May 28, 2024

You don't need to compute the conversion yourself, you can just use _resolver.GetConversion(d.Initializer).
You can always create a converted resolve result like this:

ResolveResult ResolveWithConversion(Expression expr) {
   return new ConversionResolveResult(resolver.GetExpectedType(expr), resolver.Resolve(expr), resolver.GetConversion(expr));
}

from nrefactory.

erik-kallen avatar erik-kallen commented on May 28, 2024

That ResolveWithConversion method was just what I was looking for. If I were you, I'd consider putting that in the CSharpAstResolver class.

from nrefactory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.