Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions lib/elixir/lib/code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ defmodule Code do
all imports, requires and aliases defined in the current environment
will be automatically carried over:

iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
iex> require Integer
iex> {result, binding} = Code.eval_string("if Integer.is_odd(a), do: a + b", [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
Expand Down Expand Up @@ -975,12 +976,18 @@ defmodule Code do
the code representation (AST). While the formatter can preserve code
comments between expressions and function arguments, the formatter
cannot currently preserve them around operators. For example, the following
code will move the code comments to before the operator usage:
code:

foo() ||
# also check for bar
bar()

will move the code comments to before the operator usage:

# also check for bar
foo() ||
bar()

In some situations, code comments can be seen as ambiguous by the formatter.
For example, the comment in the anonymous function below

Expand Down Expand Up @@ -1247,6 +1254,14 @@ defmodule Code do
* atoms used to represent single-letter sigils like `:sigil_X`
(but multi-letter sigils like `:sigil_XYZ` are encoded).

## Examples

iex> Code.string_to_quoted("1 + 3")
{:ok, {:+, [line: 1], [1, 3]}}

iex> Code.string_to_quoted("1 \ 3")
{:error, {[line: 1, column: 4], "syntax error before: ", "\"3\""}}

"""
@spec string_to_quoted(List.Chars.t(), keyword) ::
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
Expand Down Expand Up @@ -1600,8 +1615,8 @@ defmodule Code do

## Examples

Code.get_compiler_option(:debug_info)
#=> true
iex> Code.get_compiler_option(:debug_info)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is reading global state and some tests may change this.

Copy link
Contributor Author

@dmitrykleymenov dmitrykleymenov Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get it, with mix test it could be false. And put into it is even worse. Reverted, will be more careful with changes

true

"""
@doc since: "1.10.0"
Expand Down Expand Up @@ -1710,8 +1725,8 @@ defmodule Code do

## Examples

Code.put_compiler_option(:debug_info, true)
#=> :ok
iex> Code.put_compiler_option(:debug_info, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. May affect global state. We should revert.

:ok

"""
@doc since: "1.10.0"
Expand Down Expand Up @@ -2085,11 +2100,11 @@ defmodule Code do

## Examples

iex> Code.loaded?(Atom)
true
Code.loaded?(Atom)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this and use a module that is likely to already be loaded, such as Enum or String. Atom indeed may not be loaded.

#=> true

iex> Code.loaded?(NotYetLoaded)
false
Code.loaded?(NotYetLoaded)
#=> false

"""
@doc since: "1.15.0"
Expand Down