Skip to content

Commit

Permalink
Improve error reporting: Detect module declarations that incorrectly …
Browse files Browse the repository at this point in the history
…end with `=` - fixes dotnet#1214
  • Loading branch information
forki committed May 30, 2016
1 parent 02b490d commit e8d10cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/fsharp/CompileOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3235,8 +3235,12 @@ let PostParseModuleImpl (_i,defaultNamespace,isLastCompiland,filename,impl) =

| ParsedImplFileFragment.AnonModule (defs,m)->
let isLast, isExe = isLastCompiland
if not (isLast && isExe ) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (Filename.checkSuffix (String.lowercase filename))) then
errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(),trimRangeToLine m))
let lower = String.lowercase filename
if not (isLast && isExe) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (Filename.checkSuffix lower)) then
match defs with
| SynModuleDecl.NestedModule(_) :: _ -> errorR(Error(FSComp.SR.noEqualSignAfterModule(),trimRangeToLine m))
| _ -> errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(),trimRangeToLine m))

let modname = ComputeAnonModuleName (nonNil defs) defaultNamespace filename (trimRangeToLine m)
SynModuleOrNamespace(modname,true,defs,PreXmlDoc.Empty,[],None,m)

Expand All @@ -3259,8 +3263,12 @@ let PostParseModuleSpec (_i,defaultNamespace,isLastCompiland,filename,intf) =

| ParsedSigFileFragment.AnonModule (defs,m) ->
let isLast, isExe = isLastCompiland
if not (isLast && isExe) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (Filename.checkSuffix (String.lowercase filename))) then
errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(),m))
let lower = String.lowercase filename
if not (isLast && isExe) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (Filename.checkSuffix lower)) then
match defs with
| SynModuleSigDecl.NestedModule(_) :: _ -> errorR(Error(FSComp.SR.noEqualSignAfterModule(),m))
| _ -> errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(),m))

let modname = ComputeAnonModuleName (nonNil defs) defaultNamespace filename (trimRangeToLine m)
SynModuleOrNamespaceSig(modname,true,defs,PreXmlDoc.Empty,[],None,m)

Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ buildCouldNotReadVersionInfoFromMscorlib,"Could not read version from mscorlib.d
220,buildAssemblyResolutionFailed,"Assembly resolution failure at or near this location"
221,buildImplicitModuleIsNotLegalIdentifier,"The declarations in this file will be placed in an implicit module '%s' based on the file name '%s'. However this is not a valid F# identifier, so the contents will not be accessible from other files. Consider renaming the file or adding a 'module' or 'namespace' declaration at the top of the file."
222,buildMultiFileRequiresNamespaceOrModule,"Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration."
222,noEqualSignAfterModule,"Files in libraries or multiple-file applications must begin with a namespace or module declaration. When using a module declaration at the start of a file the '=' sign is not allowed. If this is a top-level module, consider removing the = to resolve this error."
223,buildMultipleToplevelModules,"This file contains multiple declarations of the form 'module SomeNamespace.SomeModule'. Only one declaration of this form is permitted in a file. Change your file to use an initial namespace declaration and/or use 'module ModuleName = ...' to define your modules."
224,buildOptionRequiresParameter,"Option requires parameter: %s"
225,buildCouldNotFindSourceFile,"Source file '%s' could not be found"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This testcase will emit a warning even though it contains a module, becase
// it is a nested module and not a 'real' module

//<Expects status="error" span="(7,1)" id="FS0222">Files in libraries or multiple-file applications must begin with a namespace or module declaration, e\.g\. 'namespace SomeNamespace\.SubNamespace' or 'module SomeNamespace\.SomeModule'\. Only the last source file of an application may omit such a declaration\.$</Expects>
//<Expects status="error" span="(7,1)" id="FS0222">Files in libraries or multiple-file applications must begin with a namespace or module declaration. When using a module declaration at the start of a file the '=' sign is not allowed. If this is a top-level module, consider removing the = to resolve this error.</Expects>

module ANestedModule =

Expand Down
7 changes: 7 additions & 0 deletions tests/fsharpqa/Source/Warnings/ModuleWithoutNamespace.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// #Warnings
//<Expects status="error" span="(4,1)" id="FS0222">Files in libraries or multiple-file applications must begin with a namespace or module declaration. When using a module declaration at the start of a file the '=' sign is not allowed. If this is a top-level module, consider removing the = to resolve this error.</Expects>

module X =
let x = 1

0
1 change: 1 addition & 0 deletions tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs
SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs
SOURCE=AssignmentOnImmutable.fs # AssignmentOnImmutable.fs
SOURCE=ModuleWithoutNamespace.fs # ModuleWithoutNamespace.fs
SOURCE=UpcastInsteadOfDowncast.fs # UpcastInsteadOfDowncast.fs
SOURCE=UpcastFunctionInsteadOfDowncast.fs # UpcastFunctionInsteadOfDowncast.fs
SOURCE=DowncastInsteadOfUpcast.fs # DowncastInsteadOfUpcast.fs
Expand Down

0 comments on commit e8d10cd

Please sign in to comment.