Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Last active October 26, 2018 15:07
Show Gist options
  • Save azyobuzin/00d4d710138e7b7cb290166d4458c25c to your computer and use it in GitHub Desktop.
Save azyobuzin/00d4d710138e7b7cb290166d4458c25c to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine("Roslyn v" + typeof(CSharpCompilation).Assembly.GetName().Version);
var testSource = @"using System;
class Program
{
static void Main(string[] args)
{
var (foo, bar) = F(() =>
{
return F(() =>
{
var foo = 0;
var bar = 1;
return (foo, bar);
});
});
}
static T F<T>(Func<T> x) => x();
}";
var compilation = CSharpCompilation.Create(
"CS0136Test",
new SyntaxTree[] { CSharpSyntaxTree.ParseText(testSource) },
new MetadataReference[] { MetadataReference.CreateFromFile(typeof(string).Assembly.Location) }
);
Console.WriteLine("Library");
foreach (var diag in compilation.GetDiagnostics())
{
Console.WriteLine(diag);
}
var testScript = @"using System;
T F<T>(Func<T> x) => x();
var (foo, bar) = F(() =>
{
return F(() => {
var foo = 0;
var bar = 1;
return (foo, bar);
});
});";
var scriptCompilation = CSharpCompilation.CreateScriptCompilation(
"CS0136Test",
CSharpSyntaxTree.ParseText(testScript, new CSharpParseOptions(kind: SourceCodeKind.Script)),
new MetadataReference[] { MetadataReference.CreateFromFile(typeof(string).Assembly.Location) }
);
Console.WriteLine();
Console.WriteLine("Script");
foreach (var diag in scriptCompilation.GetDiagnostics())
{
Console.WriteLine(diag);
}
}
}
/*
Roslyn v2.9.0.0
Library
(10,21): error CS0136: A local or parameter named 'foo' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
(11,21): error CS0136: A local or parameter named 'bar' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
Script
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment