Last active
December 19, 2016 19:58
-
-
Save lulu-berlin/b1555bc58212e1b44850 to your computer and use it in GitHub Desktop.
An FSI script to reproduce an error in FSharp.Compiler.Service: doesn't find identifiers beginning with an underscore (with double backticks)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "FSharp.Compiler.Service.dll" | |
open System.IO | |
open Microsoft.FSharp.Compiler.SourceCodeServices | |
let checker = FSharpChecker.Create () | |
let filesource1 = """ | |
module Abc | |
let x = 1 | |
let y = x + 1 | |
printfn "%d" x | |
""" | |
let filename1 = @"try1.fs" | |
File.WriteAllText(filename1, filesource1) | |
let filesource2 = """ | |
module Def | |
let ``_x`` = 1 | |
let y = ``_x`` + 1 | |
printfn "%d" ``_x`` | |
""" | |
let filename2 = @"try2.fs" | |
File.WriteAllText(filename2, filesource2) | |
let projfilename = @"try.fsproj" | |
let dllName = @"try.dll" | |
let projectOptions = | |
let sysLib nm = | |
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows | |
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + | |
@"\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\" + nm + ".dll" | |
else | |
let sysDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() | |
let (++) a b = System.IO.Path.Combine(a,b) | |
sysDir ++ nm + ".dll" | |
let fsCore4300() = | |
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows | |
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + | |
@"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll" | |
else | |
sysLib "FSharp.Core" | |
checker.GetProjectOptionsFromCommandLineArgs | |
(projfilename, | |
[| yield "--simpleresolution" | |
yield "--noframework" | |
yield "--debug:full" | |
yield "--define:DEBUG" | |
yield "--optimize-" | |
yield "--out:" + dllName | |
yield "--doc:test.xml" | |
yield "--warn:3" | |
yield "--fullpaths" | |
yield "--flaterrors" | |
yield "--target:library" | |
yield filename1 | |
yield filename2 | |
let references = | |
[ sysLib "mscorlib" | |
sysLib "System" | |
sysLib "System.Core" | |
fsCore4300() ] | |
for r in references do | |
yield "-r:" + r |]) | |
let backgroundParseResults1, backgroundTypedParse1 = | |
checker.GetBackgroundCheckResultsForFileInProject(filename1, projectOptions) | |
|> Async.RunSynchronously | |
let parseResults1, checkAnswer1 = | |
checker.ParseAndCheckFileInProject(filename1, 0, filesource1, projectOptions) | |
|> Async.RunSynchronously | |
let checkResults1 = | |
match checkAnswer1 with | |
| FSharpCheckFileAnswer.Succeeded x -> x | |
| _ -> failwith "unexpected aborted" | |
let allUsesOfAllSymbolsInFile1 = | |
checkResults1.GetAllUsesOfAllSymbolsInFile() | |
|> Async.RunSynchronously | |
let backgroundParseResults2, backgroundTypedParse2 = | |
checker.GetBackgroundCheckResultsForFileInProject(filename2, projectOptions) | |
|> Async.RunSynchronously | |
let parseResults2, checkAnswer2 = | |
checker.ParseAndCheckFileInProject(filename2, 0, filesource2, projectOptions) | |
|> Async.RunSynchronously | |
let checkResults2 = | |
match checkAnswer2 with | |
| FSharpCheckFileAnswer.Succeeded x -> x | |
| _ -> failwith "unexpected aborted" | |
let allUsesOfAllSymbolsInFile2 = | |
checkResults2.GetAllUsesOfAllSymbolsInFile() | |
|> Async.RunSynchronously | |
printfn "try1: with identifier x" | |
for x in allUsesOfAllSymbolsInFile1 do | |
printfn "Symbol: %A" x.Symbol | |
printfn "RangeAlternate: %A" x.RangeAlternate | |
printfn "" | |
printfn "" | |
printfn "try2: with identifier ``_x``" | |
for x in allUsesOfAllSymbolsInFile2 do | |
printfn "Symbol: %A" x.Symbol | |
printfn "RangeAlternate: %A" x.RangeAlternate | |
printfn "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment