[m-rev.] for review: Only search for source file matching fully qualified module name.

Peter Wang novalazy at gmail.com
Mon Jan 13 15:12:52 AEDT 2020


Delete now-obsolete code to search for a module in files matching
partially qualified versions of the module name.

compiler/find_module.m:
    As above.

compiler/read_modules.m:
compiler/write_deps_file.m:
    Conform to changes.

tests/invalid/bad_module_name.err_exp
tests/invalid/bad_module_name.m
tests/invalid/bad_module_name_sub.m -> tests/invalid/bad_module_name.sub.m:
    Rename sub-module and its source file so that the source file will
    be found.

diff --git a/compiler/find_module.m b/compiler/find_module.m
index f25bc7c86..984485725 100644
--- a/compiler/find_module.m
+++ b/compiler/find_module.m
@@ -1,7 +1,7 @@
 %-----------------------------------------------------------------------------e
 % vim: ft=mercury ts=4 sw=4 et
 %-----------------------------------------------------------------------------e
-% Copyright (C) 2014 The Mercury team.
+% Copyright (C) 2014, 2016-2017, 2019-2020 The Mercury team.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -25,33 +25,26 @@
 
 %---------------------------------------------------------------------------%
 
-    % search_for_module_source(Globals, Dirs, InterfaceDirs,
-    %   ModuleName, FoundSourceFile, !IO):
+    % search_for_module_source(Globals, Dirs, ModuleName, FoundSourceFile,
+    %   !IO):
     %
     % Look for the source for ModuleName in Dirs. If found, return the
     % (relative or absolute) path name of the source file that contains
     % the module.
     %
-    % This will also search for files matching partially qualified versions
-    % of ModuleName, dropping qualifiers outermost to innermost, but only if
-    % a more qualified `.m' or `.int' file doesn't exist in InterfaceDirs.
-    % For example, module foo.bar.baz can be found in foo.bar.m, bar.baz.m
-    % or bar.m.
-    %
 :- pred search_for_module_source(globals::in, list(dir_name)::in,
-    list(dir_name)::in, module_name::in,
-    maybe_error(file_name)::out, io::di, io::uo) is det.
+    module_name::in, maybe_error(file_name)::out, io::di, io::uo) is det.
 
-    % search_for_module_source_and_stream(Globals, Dirs, InterfaceDirs,
-    %   ModuleName, FoundSourceFileNameAndStream, !IO):
+    % search_for_module_source_and_stream(Globals, Dirs, ModuleName,
+    %   FoundSourceFileNameAndStream, !IO):
     %
     % As search_for_module_source, but if we find the file, then return
     % not just its path name, but also an open stream reading from it.
     % Closing that stream is the caller's responsibility.
     %
 :- pred search_for_module_source_and_stream(globals::in, list(dir_name)::in,
-    list(dir_name)::in, module_name::in,
-    maybe_error(path_name_and_stream)::out, io::di, io::uo) is det.
+    module_name::in, maybe_error(path_name_and_stream)::out, io::di, io::uo)
+    is det.
 
     % Read the first item from the given file to find the module name.
     %
@@ -72,10 +65,9 @@
 
 %---------------------------------------------------------------------------%
 
-search_for_module_source(Globals, Dirs, InterfaceDirs, ModuleName,
-        MaybeFileName, !IO) :-
-    search_for_module_source_and_stream(Globals, Dirs, InterfaceDirs,
-        ModuleName, MaybeFileNameAndStream, !IO),
+search_for_module_source(Globals, Dirs, ModuleName, MaybeFileName, !IO) :-
+    search_for_module_source_and_stream(Globals, Dirs, ModuleName,
+        MaybeFileNameAndStream, !IO),
     (
         MaybeFileNameAndStream =
             ok(path_name_and_stream(SourceFileName, SourceStream)),
@@ -86,141 +78,18 @@ search_for_module_source(Globals, Dirs, InterfaceDirs, ModuleName,
         MaybeFileName = error(Msg)
     ).
 
-search_for_module_source_and_stream(Globals, Dirs, InterfaceDirs, ModuleName,
+search_for_module_source_and_stream(Globals, Dirs, ModuleName,
         MaybeFileNameAndStream, !IO) :-
-    search_for_module_source_dropping_qualifiers(Globals,
-        Dirs, ModuleName, MaybeFileNameAndStream0, !IO),
-    (
-        MaybeFileNameAndStream0 =
-            ok(path_name_and_stream(SourceFileName, SourceStream)),
-        ( if
-            string.remove_suffix(dir.basename(SourceFileName),
-                ".m", SourceFileBaseName),
-            file_name_to_module_name(SourceFileBaseName, SourceFileModuleName),
-            ModuleName \= SourceFileModuleName
-        then
-            % The module name doesn't match the file name. Return an error
-            % if there is a more qualified matching `.m' or `.int' file in
-            % the interface search path. This avoids having a file `read.m'
-            % in the current directory prevent the compiler from finding
-            % `bit_buffer.read.int' in the standard library.
-            % Note that we never need to read from this more qualified file.
-
-            search_for_module_source_dropping_qualifiers(Globals,
-                InterfaceDirs, ModuleName, MaybeIFaceFileNameAndStream, !IO),
-            (
-                MaybeIFaceFileNameAndStream =
-                    ok(path_name_and_stream(_IFaceFileName, IFaceStream)),
-                io.close_input(IFaceStream, !IO)
-            ;
-                MaybeIFaceFileNameAndStream = error(_)
-            ),
-            ( if
-                MaybeIFaceFileNameAndStream =
-                    ok(path_name_and_stream(IFaceFileName, _IFaceStream)),
-                IFaceFileName \= SourceFileName,
-                string.remove_suffix(dir.basename(IFaceFileName), ".m",
-                    IFaceFileBaseName),
-                file_name_to_module_name(IFaceFileBaseName,
-                    IFaceFileModuleName),
-                partial_sym_name_matches_full(SourceFileModuleName,
-                    IFaceFileModuleName)
-            then
-                io.close_input(SourceStream, !IO),
-                Error =
-                    find_source_error(ModuleName, Dirs, yes(IFaceFileName)),
-                MaybeFileNameAndStream = error(Error)
-            else
-                module_name_to_file_name(Globals, do_not_create_dirs, ".int",
-                    ModuleName, IntFile, !IO),
-                search_for_file_returning_dir(InterfaceDirs, IntFile,
-                    MaybeIntDir, !IO),
-                ( if
-                    MaybeIntDir = ok(IntDir),
-                    IntDir \= dir.this_directory
-                then
-                    io.close_input(SourceStream, !IO),
-                    Error = find_source_error(ModuleName, Dirs,
-                        yes(IntDir/IntFile)),
-                    MaybeFileNameAndStream = error(Error)
-                else
-                    MaybeFileNameAndStream = MaybeFileNameAndStream0
-                )
-            )
-        else
-            MaybeFileNameAndStream = MaybeFileNameAndStream0
-        )
-    ;
-        MaybeFileNameAndStream0 = error(_),
-        MaybeFileNameAndStream = MaybeFileNameAndStream0
-    ).
-
-%------------%
-
-:- pred search_for_module_source_dropping_qualifiers(globals::in,
-    list(dir_name)::in, module_name::in,
-    maybe_error(path_name_and_stream)::out, io::di, io::uo) is det.
-
-search_for_module_source_dropping_qualifiers(Globals, Dirs, ModuleName,
-        MaybeErrorFileNameAndStream, !IO) :-
-    search_for_module_source_dropping_qualifiers_loop(Globals, Dirs,
-        ModuleName, MaybeFileNameAndStream, !IO),
-    (
-        MaybeFileNameAndStream = yes(FileNameAndStream),
-        MaybeErrorFileNameAndStream = ok(FileNameAndStream)
-    ;
-        MaybeFileNameAndStream = no,
-        Error = find_source_error(ModuleName, Dirs, no),
-        MaybeErrorFileNameAndStream = error(Error)
-    ).
-
-:- pred search_for_module_source_dropping_qualifiers_loop(globals::in,
-    list(dir_name)::in, module_name::in, maybe(path_name_and_stream)::out,
-    io::di, io::uo) is det.
-
-search_for_module_source_dropping_qualifiers_loop(Globals, Dirs,
-        PartialModuleName0, MaybeFileNameAndStream, !IO) :-
     module_name_to_file_name(Globals, do_not_create_dirs, ".m",
-        PartialModuleName0, FileName0, !IO),
+        ModuleName, FileName0, !IO),
     search_for_file_and_stream(Dirs, FileName0, MaybeFileNameAndStream0, !IO),
     (
-        MaybeFileNameAndStream0 = ok(FileNameAndStream0),
-        MaybeFileNameAndStream = yes(FileNameAndStream0)
+        MaybeFileNameAndStream0 = ok(_),
+        MaybeFileNameAndStream = MaybeFileNameAndStream0
     ;
         MaybeFileNameAndStream0 = error(_),
-        ( if
-            drop_outermost_qualifier(PartialModuleName0, PartialModuleName1)
-        then
-            search_for_module_source_dropping_qualifiers_loop(Globals, Dirs,
-                PartialModuleName1, MaybeFileNameAndStream, !IO)
-        else
-            MaybeFileNameAndStream = no
-        )
-    ).
-
-%------------%
-
-:- pred drop_outermost_qualifier(module_name::in, module_name::out) is semidet.
-
-drop_outermost_qualifier(SymName, DroppedQualifierSymName) :-
-    SymName = qualified(ParentModuleName, ChildName), 
-    drop_outermost_qualifier_loop(ParentModuleName, ChildName,
-        DroppedQualifierSymName).
-
-:- pred drop_outermost_qualifier_loop(module_name::in, string::in,
-    module_name::out) is det.
-
-drop_outermost_qualifier_loop(ParentModuleName, ChildName,
-        DroppedQualifierSymName) :-
-    (
-        ParentModuleName = unqualified(_ParentName),
-        DroppedQualifierSymName = unqualified(ChildName)
-    ;
-        ParentModuleName = qualified(GrandParentModuleName, ParentName),
-        drop_outermost_qualifier_loop(GrandParentModuleName, ParentName,
-            DroppedQualifierGrandParentModuleName),
-        DroppedQualifierSymName =
-            qualified(DroppedQualifierGrandParentModuleName, ChildName)
+        Error = find_source_error(ModuleName, Dirs, no),
+        MaybeFileNameAndStream = error(Error)
     ).
 
 %------------%
diff --git a/compiler/read_modules.m b/compiler/read_modules.m
index 4fdcd38cf..89fe84ebd 100644
--- a/compiler/read_modules.m
+++ b/compiler/read_modules.m
@@ -248,12 +248,11 @@ read_module_src(Globals, Descr, IgnoreErrors, Search,
         ReadModuleAndTimestamps, MaybeTimestamp,
         ParseTreeSrc, Specs, Errors, !IO) :-
     read_module_begin(Globals, Descr, Search, ModuleName, fk_src,
-        FileName0, VeryVerbose, InterfaceSearchDirs, SearchDirs, !IO),
+        FileName0, VeryVerbose, SearchDirs, !IO),
     % For `.m' files we need to deal with the case where the module name
-    % does not match the file name, or where a partial match occurs
-    % in the current directory but the full match occurs in a search directory.
+    % does not match the file name.
     search_for_module_source_and_stream(Globals, SearchDirs,
-        InterfaceSearchDirs, ModuleName, MaybeFileNameAndStream, !IO),
+        ModuleName, MaybeFileNameAndStream, !IO),
     actually_read_module_src(Globals, ModuleName, ExpectationContexts,
         MaybeFileNameAndStream, ReadModuleAndTimestamps, MaybeTimestampRes,
         ParseTreeSrc0, ModuleSpecs, Errors, !IO),
@@ -307,7 +306,7 @@ read_module_int(Globals, Descr, IgnoreErrors, Search, ModuleName, IntFileKind,
         FileName, ReadModuleAndTimestamps, MaybeTimestamp,
         ParseTreeInt, Specs, Errors, !IO) :-
     read_module_begin(Globals, Descr, Search, ModuleName, fk_int(IntFileKind),
-        FileName0, VeryVerbose, _InterfaceSearchDirs, SearchDirs, !IO),
+        FileName0, VeryVerbose, SearchDirs, !IO),
     search_for_file_and_stream(SearchDirs, FileName0,
         MaybeFileNameAndStream, !IO),
     actually_read_module_int(IntFileKind, Globals, ModuleName, [],
@@ -321,10 +320,10 @@ read_module_int(Globals, Descr, IgnoreErrors, Search, ModuleName, IntFileKind,
 
 :- pred read_module_begin(globals::in, string::in,
     maybe_search::in, module_name::in, file_kind::in, file_name::out,
-    bool::out, list(string)::out, list(string)::out, io::di, io::uo) is det.
+    bool::out, list(string)::out, io::di, io::uo) is det.
 
 read_module_begin(Globals, Descr, Search, ModuleName, FileKind,
-        FileName0, VeryVerbose, InterfaceSearchDirs, SearchDirs, !IO) :-
+        FileName0, VeryVerbose, SearchDirs, !IO) :-
     Extension = file_kind_to_extension(FileKind),
     (
         Search = do_search,
@@ -340,11 +339,10 @@ read_module_begin(Globals, Descr, Search, ModuleName, FileKind,
     maybe_write_string(VeryVerbose, Msg, !IO),
     maybe_flush_output(VeryVerbose, !IO),
 
-    globals.lookup_accumulating_option(Globals, search_directories,
-        InterfaceSearchDirs),
     (
         Search = do_search,
-        SearchDirs = InterfaceSearchDirs
+        globals.lookup_accumulating_option(Globals, search_directories,
+            SearchDirs)
     ;
         Search = do_not_search,
         SearchDirs = [dir.this_directory]
diff --git a/compiler/write_deps_file.m b/compiler/write_deps_file.m
index 0511f2d45..6f7d4210b 100644
--- a/compiler/write_deps_file.m
+++ b/compiler/write_deps_file.m
@@ -2235,8 +2235,8 @@ get_both_opt_deps(Globals, BuildOptFiles, IntermodDirs, [Dep | Deps],
         !:OptDeps, !:TransOptDeps, !IO),
     (
         BuildOptFiles = yes,
-        search_for_module_source(Globals, IntermodDirs, IntermodDirs,
-            Dep, MaybeFileName, !IO),
+        search_for_module_source(Globals, IntermodDirs, Dep, MaybeFileName,
+            !IO),
         (
             MaybeFileName = ok(_),
             !:OptDeps = [Dep | !.OptDeps],
@@ -2282,8 +2282,7 @@ get_opt_deps(Globals, BuildOptFiles, IntermodDirs, Suffix, [Dep | Deps],
         !:OptDeps, !IO),
     (
         BuildOptFiles = yes,
-        search_for_module_source(Globals, IntermodDirs, IntermodDirs,
-            Dep, Result1, !IO),
+        search_for_module_source(Globals, IntermodDirs, Dep, Result1, !IO),
         (
             Result1 = ok(_),
             !:OptDeps = [Dep | !.OptDeps],
diff --git a/tests/invalid/bad_module_name.err_exp b/tests/invalid/bad_module_name.err_exp
index ffee36974..d1d5276ea 100644
--- a/tests/invalid/bad_module_name.err_exp
+++ b/tests/invalid/bad_module_name.err_exp
@@ -1,5 +1,5 @@
-bad_module_name_sub.m:008: Error: file `bad_module_name_sub.m' contains the
-bad_module_name_sub.m:008:   wrong module. Expected module
-bad_module_name_sub.m:008:   `bad_module_name.bad_module_name_sub', found
-bad_module_name_sub.m:008:   module `wrong_module_name'.
+bad_module_name.sub.m:008: Error: file `bad_module_name.sub.m' contains the
+bad_module_name.sub.m:008:   wrong module. Expected module
+bad_module_name.sub.m:008:   `bad_module_name.sub', found module
+bad_module_name.sub.m:008:   `wrong_module_name'.
 bad_module_name.m:010:   The expected name is specified here.
diff --git a/tests/invalid/bad_module_name.m b/tests/invalid/bad_module_name.m
index b73abdda5..23f27b513 100644
--- a/tests/invalid/bad_module_name.m
+++ b/tests/invalid/bad_module_name.m
@@ -7,4 +7,4 @@
     --->    f1
     ;       f2.
 
-:- include_module bad_module_name_sub.
+:- include_module sub.
diff --git a/tests/invalid/bad_module_name_sub.m b/tests/invalid/bad_module_name.sub.m
similarity index 100%
rename from tests/invalid/bad_module_name_sub.m
rename to tests/invalid/bad_module_name.sub.m
-- 
2.24.1



More information about the reviews mailing list