Skip to content

Commit b72cac2

Browse files
committed
Add some testcases to compiler to verify that overriding really happens
1 parent 634dd37 commit b72cac2

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

lib/compiler/test/misc_SUITE.erl

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@
2020

2121
-export([all/1,init_per_testcase/2,fin_per_testcase/2,
2222
tobias/1,empty_string/1,md5/1,silly_coverage/1,
23-
confused_literals/1,integer_encoding/1]).
23+
confused_literals/1,integer_encoding/1,override_bif/1]).
2424

2525
-include("test_server.hrl").
2626

27+
%% For the override_bif testcase.
28+
%% NB, no other testcases in this testsuite can use these without erlang:prefix!
29+
-compile({no_auto_import,[abs/1]}).
30+
-compile({no_auto_import,[binary_part/3]}).
31+
-compile({no_auto_import,[binary_part/2]}).
32+
-import(test_lib,[binary_part/2]).
33+
34+
%% This should do no harm (except for fun byte_size/1 which does not, by design, work with import
35+
-compile({no_auto_import,[byte_size/1]}).
36+
-import(erlang,[byte_size/1]).
37+
38+
39+
2740
%% Include an opaque declaration to cover the stripping of
2841
%% opaque types from attributes in v3_kernel.
2942
-opaque misc_SUITE_test_cases() :: [atom()].
@@ -42,7 +55,39 @@ fin_per_testcase(Case, Config) when is_atom(Case), is_list(Config) ->
4255
all(suite) ->
4356
test_lib:recompile(?MODULE),
4457
[tobias,empty_string,md5,silly_coverage,confused_literals,
45-
integer_encoding].
58+
integer_encoding, override_bif].
59+
60+
61+
%%
62+
%% Functions that override new and old bif's
63+
%%
64+
abs(_N) ->
65+
dummy_abs.
66+
67+
binary_part(_,_,_) ->
68+
dummy_bp.
69+
70+
% Make sure that auto-imported BIF's are overridden correctly
71+
72+
override_bif(suite) ->
73+
[];
74+
override_bif(doc) ->
75+
["Test dat local functions and imports override auto-imported BIFs."];
76+
override_bif(Config) when is_list(Config) ->
77+
?line dummy_abs = abs(1),
78+
?line dummy_bp = binary_part(<<"hello">>,1,1),
79+
?line dummy = binary_part(<<"hello">>,{1,1}),
80+
?line 1 = erlang:abs(1),
81+
?line <<"e">> = erlang:binary_part(<<"hello">>,1,1),
82+
?line <<"e">> = erlang:binary_part(<<"hello">>,{1,1}),
83+
F = fun(X) when byte_size(X) =:= 4 ->
84+
four;
85+
(X) ->
86+
byte_size(X)
87+
end,
88+
?line four = F(<<1,2,3,4>>),
89+
?line 5 = F(<<1,2,3,4,5>>),
90+
ok.
4691

4792
%% A bug reported by Tobias Lindahl for a development version of R11B.
4893

lib/compiler/test/test_lib.erl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
-module(test_lib).
2020

2121
-include("test_server.hrl").
22-
23-
-export([recompile/1,opt_opts/1,get_data_dir/1,smoke_disasm/1,p_run/2]).
22+
-compile({no_auto_import,[binary_part/2]}).
23+
-export([recompile/1,opt_opts/1,get_data_dir/1,smoke_disasm/1,p_run/2,binary_part/2]).
2424

2525
recompile(Mod) when is_atom(Mod) ->
2626
case whereis(cover_server) of
@@ -104,3 +104,7 @@ p_run_loop(Test, List, N, Refs0, Errors0, Ws0) ->
104104
Refs = Refs0 -- [Ref],
105105
p_run_loop(Test, List, N, Refs, Errors, Ws)
106106
end.
107+
108+
%% This is for the misc_SUITE:override_bif testcase
109+
binary_part(_A,_B) ->
110+
dummy.

0 commit comments

Comments
 (0)