Skip to content

Commit e0d7bb4

Browse files
committed
Fix merl:compile_and_load/1 with comments
Use erl_syntax:revert_forms/1 instead of manually reverting each form to properly handle syntax trees containing comments. The manual approach failed when comment nodes were passed to the compiler. Add test case to verify compile_and_load works with various comment placements in quoted code.
1 parent 8fb9b41 commit e0d7bb4

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/syntax_tools/src/merl.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ compile(Code, Options) when not is_list(Code)->
429429
_ -> compile([Code], Options)
430430
end;
431431
compile(Code, Options0) when is_list(Options0) ->
432-
Forms = [erl_syntax:revert(F) || F <- Code],
432+
Forms = erl_syntax:revert_forms(Code),
433433
Options = [verbose, report_errors, report_warnings, binary | Options0],
434434
compile:noenv_forms(Forms, Options).
435435

lib/syntax_tools/test/merl_SUITE.erl

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
%% ``Licensed under the Apache License, Version 2.0 (the "License");
1+
%%
2+
%% %CopyrightBegin%
3+
%%
4+
%% SPDX-License-Identifier: Apache-2.0
5+
%%
6+
%% Copyright Ericsson AB 1996-2025. All Rights Reserved.
7+
%%
8+
%% Licensed under the Apache License, Version 2.0 (the "License");
29
%% you may not use this file except in compliance with the License.
310
%% You may obtain a copy of the License at
411
%%
@@ -9,11 +16,9 @@
916
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1017
%% See the License for the specific language governing permissions and
1118
%% limitations under the License.
12-
%%
13-
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
14-
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
15-
%% AB. All Rights Reserved.''
16-
%%
19+
%%
20+
%% %CopyrightEnd%
21+
%%
1722
-module(merl_SUITE).
1823

1924
-include_lib("common_test/include/ct.hrl").
@@ -25,19 +30,21 @@
2530
-include_lib("eunit/include/eunit.hrl").
2631

2732
%% Test server specific exports
28-
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
33+
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
2934
init_per_group/2,end_per_group/2]).
3035

3136
%% Test cases
3237
-export([merl_smoke_test/1,
33-
transform_parse_error_test/1, otp_15291/1]).
38+
transform_parse_error_test/1, otp_15291/1,
39+
compile_and_load_with_comments/1]).
3440

3541
suite() -> [{ct_hooks,[ts_install_cth]}].
3642

3743
all() ->
3844
[merl_smoke_test,
3945
transform_parse_error_test,
40-
otp_15291].
46+
otp_15291,
47+
compile_and_load_with_comments].
4148

4249
groups() ->
4350
[].
@@ -112,6 +119,18 @@ otp_15291(_Config) ->
112119
{clause,A1,[{var,A1,'_'}],[],[{atom,A1,ok}]} = C1,
113120
ok.
114121

122+
compile_and_load_with_comments(_Config) ->
123+
?assertMatch({ok, _},
124+
merl:compile_and_load(merl:quote(
125+
~"-module(merl_test_module_1)."))),
126+
?assertMatch({ok, _},
127+
merl:compile_and_load(merl:quote(
128+
~"-module(merl_test_module_2). % comment"))),
129+
?assertMatch({ok, _},
130+
merl:compile_and_load(merl:quote(
131+
~"\n-module(merl_test_module_3).\n% comment"))),
132+
ok.
133+
115134
%% utilities
116135

117136
f(Ts) when is_list(Ts) ->

0 commit comments

Comments
 (0)