Skip to content

Conversation

williamthome
Copy link
Contributor

@williamthome williamthome commented Sep 25, 2025

Problem

merl:compile_and_load/1 fails when quoted code contains comment lines:

$ erl
Erlang/OTP 28 [erts-16.0.3] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [jit:ns]

Eshell V16.0.3 (press Ctrl+G to abort, type help(). for help)
1> Q = merl:quote(~"""
   -module(foo).
   % comment
   """).
[{attribute,1,module,foo},
 {tree,comment,{attr,2,[],none},{comment,0,[" comment"]}}]
2> merl:compile_and_load(Q).
: internal error in pass lint_module:
exception error: no function clause matching erl_lint:function_state({tree,comment,{attr,2,[],none},{comment,0,[" comment"]}},
 {lint,function,foo,[],
       {2,{{module_info,1},{{module_info,0},nil,nil},nil}},
       [],#{},
       [binary,verbose,report_errors,report_warnings,binary],
       #{},
       {0,nil},
       {set,{0,nil}},
       {3,
        {{module_info,1},{{module_info,0},nil,nil},{{record_info,2},nil,nil}}},
       [],0,[],[],
       {0,nil},
       [],[],1,
       [behaviours,bif_clash,conflicting_behaviours,deprecated_callback,
        deprecated_function,deprecated_type,export_all,
        ill_defined_behaviour_callbacks,ill_defined_optional_callbacks,
        match_float_zero,nif_inline,obsolete_guard,redefined_builtin_type,
        removed,shadow_vars,undefined_behaviour,undefined_behaviour_callbacks,
        undefined_behaviour_func,underscore_match,unexported_function,
        unused_function,unused_record,unused_type,unused_vars,update_literal],
       [],[],[],[],false,false,
       [{{module_info,0},0},{{module_info,1},0},{{record_info,2},0}],
       undefined,
       {usage,#{{module_info,1} =>
                    [{module_info,0},{module_info,1},{record_info,2}]},
              [],
              {0,nil},
              #{}},
       #{},#{},#{},#{},#{},
       {0,nil},
       #{'maybe' => maybe_expr,'else' => maybe_expr},
       none,guard,false,false,false}) (erl_lint.erl:1137)
  in function  lists:foldl_1/3 (lists.erl:2471)
  in call from erl_lint:forms/2 (erl_lint.erl:949)
  in call from erl_lint:module/3 (erl_lint.erl:750)
  in call from compile:lint_module/2 (compile.erl:2177)
  in call from compile:fold_comp/4 (compile.erl:1239)
  in call from compile:internal_comp/5 (compile.erl:1214)
  in call from compile:'-internal_fun/2-anonymous-0-'/2 (compile.erl:1040)
error

Fix

Replace manual form reversion with erl_syntax:revert_forms/1 in merl:compile/2:

- Forms = [erl_syntax:revert(F) || F <- Code],
+ Forms = erl_syntax:revert_forms(Code),

Test

Added test covering standalone comments, inline comments, and mixed scenarios.

Copy link
Contributor

github-actions bot commented Sep 25, 2025

CT Test Results

  2 files   13 suites   4m 14s ⏱️
117 tests 113 ✅ 4 💤 0 ❌
133 runs  129 ✅ 4 💤 0 ❌

Results for commit e0d7bb4.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

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.
@williamthome williamthome force-pushed the fix/merl-compile-and-load-comments branch from 1ea3fe2 to e0d7bb4 Compare September 26, 2025 00:48
@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Sep 26, 2025
@bjorng bjorng self-assigned this Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants