Comments (7)
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.
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.
Also, I guess that due to this, find references will not find any op_Implicit that is used during variable initialization.
from nrefactory.
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 ConversionResolveResult
s; it directly looks at all conversions found by the resolver.
from nrefactory.
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.
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.
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)
- Resolving members with CSHarpTypeResolveContext
- The Online Converter untranslates event handler assignments.
- The Online Converter messes up object initializers HOT 11
- DocumentScript.FormatText is called during dispose HOT 1
- ParserError with DataRowVersion HOT 4
- Interpolated Strings: Compiler error when an interpolated string is too long HOT 1
- Make "base" and "this" constructor keywords as identifiers HOT 2
- CreateNewOverrideCompletionData does not get called
- Is it possible to force NRefactory to use .NET 3.5 instead of 4 when analyzing code?
- How to get controllers and actions attributes without using reflection
- Format issue when TextEditorOptions.TabsToSpaces is false
- CodeConverter service not available HOT 3
- An item with the same key has already been added.
- CodeConverter service again not available HOT 15
- Upgrade to latest Cecil HOT 1
- XmlDocumentationProvider - "output char buffer is too small to contain the decoded characters"
- How to implement C# 6.0+ support HOT 4
- How to implement NRefactory HOT 2
- Error on saving the solution HOT 2
- This project should be marked as archived HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nrefactory.