[m-rev.] diff: fix a bug in io.set_line_number/3
Julien Fischer
jfischer at opturion.com
Fri Mar 13 14:07:39 AEDT 2026
Fix a bug in io.set_line_number/3.
Due to a copy-and-paste error introduced in commit 05fd615471,
set_line_number/2 was incorrectly setting the line number of the
current output stream, not the current input stream.
library/io.m:
Fix the above bug.
tests/hard_coded/Mmakefile:
tests/hard_coded/bad_set_line_number.{m,exp}:
Add a regression test.
Julien.
diff --git a/library/io.m b/library/io.m
index bc9b1e4c9..8baa3e4b8 100644
--- a/library/io.m
+++ b/library/io.m
@@ -2776,7 +2776,7 @@ get_line_number(text_input_stream(Stream),
LineNum, !IO) :-
%---------------------%
set_line_number(LineNum, !IO) :-
- output_stream_2(Stream, !IO),
+ input_stream_2(Stream, !IO),
set_input_line_number_2(Stream, LineNum, !IO).
set_line_number(text_input_stream(Stream), LineNum, !IO) :-
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 6ed7aec99..68cb61346 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -24,6 +24,7 @@ ORDINARY_PROGS = \
array_test_1 \
array_test_2 \
array_unify_compare \
+ bad_set_line_number \
bag_various \
bidirectional \
bigtest \
diff --git a/tests/hard_coded/bad_set_line_number.exp
b/tests/hard_coded/bad_set_line_number.exp
new file mode 100644
index 000000000..32311d713
--- /dev/null
+++ b/tests/hard_coded/bad_set_line_number.exp
@@ -0,0 +1,4 @@
+Input line number before set_line_number: 1.
+Input line number after set_line_number(100): 100.
+Output line number: 3.
+PASS: set_line_number correctly set input line number.
diff --git a/tests/hard_coded/bad_set_line_number.m
b/tests/hard_coded/bad_set_line_number.m
new file mode 100644
index 000000000..731bb8590
--- /dev/null
+++ b/tests/hard_coded/bad_set_line_number.m
@@ -0,0 +1,48 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%---------------------------------------------------------------------------%
+% Regression test for a problem where io.set_line_number/3 was incorrectly
+% setting the line number of the current output stream instead of the
+% line number of the current input stream.
+%---------------------------------------------------------------------------%
+
+:- module bad_set_line_number.
+:- interface.
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module int.
+:- import_module list.
+:- import_module string.
+
+main(!IO) :-
+ % Get the current input stream's line number (should be 1 initially).
+ io.get_line_number(LineBefore, !IO),
+ io.format("Input line number before set_line_number: %d.\n",
+ [i(LineBefore)], !IO),
+
+ % Try to set the input stream's line number to 100.
+ io.set_line_number(100, !IO),
+
+ % Read back the input stream's line number.
+ io.get_line_number(LineAfter, !IO),
+ io.format("Input line number after set_line_number(100): %d.\n",
+ [i(LineAfter)], !IO),
+
+ % Also check the output stream's line number to see if it was
+ % accidentally modified instead.
+ io.get_output_line_number(OutLine, !IO),
+ io.format("Output line number: %d.\n", [i(OutLine)], !IO),
+
+ ( if LineAfter = 100 then
+ io.write_string(
+ "PASS: set_line_number correctly set input line number.\n", !IO)
+ else
+ io.write_string("FAIL: input line number is not 100.\n", !IO)
+ ).
More information about the reviews
mailing list