Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected results in otp 26 #9476

Open
WHYZN opened this issue Feb 24, 2025 · 4 comments
Open

Unexpected results in otp 26 #9476

WHYZN opened this issue Feb 24, 2025 · 4 comments
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@WHYZN
Copy link

WHYZN commented Feb 24, 2025

Describe the bug
Unexpected results

To Reproduce

-module(abc).

%% API
-export([a/1, b/1]).

-record(abc, {data}).

-record(kv, {k, v}).

a(Data) ->
	F =
		fun(Map) ->
			{maps:get(a, Map, 0),
				#kv{
					k = maps:get(a, Map, 0),
					v = maps:get(b, Map, 0)
				}}
		end,
	
	#abc{data = maps:from_list(lists:map(F, Data))}.

b(Data) ->
	F =
		fun(Map) ->
			#kv{
				k = maps:get(a, Map, 0),
				v = maps:get(b, Map, 0)
			}
		end,
	#abc{data = maps:from_list(lists:map(F, Data))}.

Expected behavior

(game_ccc_1@127.0.0.1)24> Data = [#{a => 1, b => 2}, #{a => 3, b => 4}].
[#{a => 1,b => 2},#{a => 3,b => 4}]
(game_ccc_1@127.0.0.1)25> abc:a(Data).
{abc,#{1 => {kv,1,2},3 => {kv,3,4}}}
(game_ccc_1@127.0.0.1)26> abc:b([]).
#{}
(game_ccc_1@127.0.0.1)27> abc:b(Data).
* exception error: bad argument
in function  maps:from_list/1
called as maps:from_list([{kv,1,2},{kv,3,4}])
in call from abc:b/1 (apps/mmo_server/src/role/abc.erl, line 43)

why ach:b([]). return #{} not return #abc{}
Alternatively, an error can be reported instead of receiving unexpected results

Affected versions
Erlang/OTP 26 [erts-14.2.5.4]

Additional context
Add any other context about the problem here. If you wish to attach Erlang code you can either write it directly in the post using code tags, create a gist, or attach it as a zip file to this post.

@WHYZN WHYZN added the bug Issue is reported as a bug label Feb 24, 2025
@garazdawi
Copy link
Contributor

Hello! This is not a bug in Erlang, but a bug in your code. The only input to maps:from_list/1 is a list of two tuples, but you are passing the a list of the #kv{} record. To make it work you need to update b to look like this:

b(Data) ->
	F =
		fun(Map) ->
			{
				maps:get(a, Map, 0),
				maps:get(b, Map, 0)
			}
		end,
	#abc{data = maps:from_list(lists:map(F, Data))}.

If you need more help understanding the Erlang and its APIs, please visit the https://www.erlangforums.com and people there will be happy to answer your questions.

@WHYZN
Copy link
Author

WHYZN commented Feb 24, 2025

i know,but when data is empty list,why can't return #abc{} or report error,
It's difficult to find where the error lies when the code is very long
@garazdawi

@garazdawi garazdawi reopened this Feb 24, 2025
@garazdawi
Copy link
Contributor

Aha, now I see what you mean! Yes, that is indeed a bug. The compiler seems to assume that the call to lists:map/2 will fail becase the fun will never return a correct value, but when called with the empty list it never calls the fun and thus does not fail.

@jhogberg
Copy link
Contributor

Thanks for reporting this, I've fixed it in #9481 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

3 participants