1
1
-module (elixir_rewrite ).
2
- -export ([rewrite /5 , inline /3 ]).
2
+ -export ([rewrite /6 , inline /3 ]).
3
3
-include (" elixir.hrl" ).
4
4
5
+ -define (is_literal (Arg ), (is_binary (Arg ) orelse is_number (Arg ) orelse is_atom (Arg ))).
6
+
5
7
% % Convenience variables
6
8
7
9
-define (atom , 'Elixir.Atom' ).
10
+ -define (access , 'Elixir.Access' ).
8
11
-define (enum , 'Elixir.Enum' ).
9
12
-define (float , 'Elixir.Float' ).
10
13
-define (io , 'Elixir.IO' ).
@@ -173,9 +176,17 @@ inline(_, _, _) -> false.
173
176
174
177
% % Complex rewrite rules
175
178
176
- rewrite (? string_chars , _DotMeta , 'to_string' , _Meta , [String ]) when is_binary (String ) ->
179
+ rewrite (? access , _DotMeta , 'get' , _Meta , [nil , Arg ], _Env )
180
+ when ? is_literal (Arg ) orelse (is_atom (element (1 , Arg )) andalso element (3 , Arg ) == nil ) ->
181
+ nil ;
182
+ rewrite (? access , _DotMeta , 'get' , Meta , [Arg , _ ], Env )
183
+ when ? is_literal (Arg ) orelse element (1 , Arg ) == '{}' orelse element (1 , Arg ) == '<<>>' ->
184
+ elixir_errors :compile_error (Meta , ? m (Env , file ),
185
+ " the Access syntax and calls to Access.get/2 are not available for the value: ~ts " ,
186
+ ['Elixir.Macro' :to_string (Arg )]);
187
+ rewrite (? string_chars , _DotMeta , 'to_string' , _Meta , [String ], _File ) when is_binary (String ) ->
177
188
String ;
178
- rewrite (? string_chars , DotMeta , 'to_string' , Meta , [String ]) ->
189
+ rewrite (? string_chars , DotMeta , 'to_string' , Meta , [String ], _Env ) ->
179
190
Var = {'rewrite' , Meta , 'Elixir' },
180
191
Guard = {{'.' , ? generated , [erlang , is_binary ]}, ? generated , [Var ]},
181
192
Slow = remote (? string_chars , DotMeta , 'to_string' , Meta , [Var ]),
@@ -186,9 +197,9 @@ rewrite(?string_chars, DotMeta, 'to_string', Meta, [String]) ->
186
197
{'->' , ? generated , [[Var ], Slow ]}]
187
198
}]]};
188
199
189
- rewrite (? enum , DotMeta , 'reverse' , Meta , [List ]) when is_list (List ) ->
200
+ rewrite (? enum , DotMeta , 'reverse' , Meta , [List ], _Env ) when is_list (List ) ->
190
201
remote (lists , DotMeta , 'reverse' , Meta , [List ]);
191
- rewrite (? enum , DotMeta , 'reverse' , Meta , [List ]) ->
202
+ rewrite (? enum , DotMeta , 'reverse' , Meta , [List ], _Env ) ->
192
203
Var = {'rewrite' , Meta , 'Elixir' },
193
204
Guard = {{'.' , ? generated , [erlang , is_list ]}, ? generated , [Var ]},
194
205
Slow = remote (? enum , DotMeta , 'reverse' , Meta , [Var ]),
@@ -199,7 +210,7 @@ rewrite(?enum, DotMeta, 'reverse', Meta, [List]) ->
199
210
{'->' , ? generated , [[Var ], Slow ]}]
200
211
}]]};
201
212
202
- rewrite (Receiver , DotMeta , Right , Meta , Args ) ->
213
+ rewrite (Receiver , DotMeta , Right , Meta , Args , _Env ) ->
203
214
{EReceiver , ERight , EArgs } = rewrite (Receiver , Right , Args ),
204
215
remote (EReceiver , DotMeta , ERight , Meta , EArgs ).
205
216
0 commit comments