Skip to content

Commit

Permalink
Fix use global paths in macro bodies (#14965)
Browse files Browse the repository at this point in the history
Macros inject code into other scopes. Paths are resolved in the expanded scope and there can be namespace conflicts.
This fixes non-global paths in macro bodies that expand into uncontrolled scopes where namespaces could clash.

This is a fixup for #14282 (released in 1.12.0).
  • Loading branch information
straight-shoota committed Sep 17, 2024
1 parent 80b2484 commit 47cd33b
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/crystal/pointer_linked_list.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ struct Crystal::PointerLinkedList(T)

module Node
macro included
property previous : Pointer(self) = Pointer(self).null
property next : Pointer(self) = Pointer(self).null
property previous : ::Pointer(self) = ::Pointer(self).null
property next : ::Pointer(self) = ::Pointer(self).null
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/ecr/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module ECR
# ```
macro def_to_s(filename)
def to_s(__io__ : IO) : Nil
ECR.embed {{filename}}, "__io__"
::ECR.embed {{filename}}, "__io__"
end
end

Expand Down
34 changes: 17 additions & 17 deletions src/intrinsics.cr
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ end

module Intrinsics
macro debugtrap
LibIntrinsics.debugtrap
::LibIntrinsics.debugtrap
end

def self.pause
Expand All @@ -191,15 +191,15 @@ module Intrinsics
end

macro memcpy(dest, src, len, is_volatile)
LibIntrinsics.memcpy({{dest}}, {{src}}, {{len}}, {{is_volatile}})
::LibIntrinsics.memcpy({{dest}}, {{src}}, {{len}}, {{is_volatile}})
end

macro memmove(dest, src, len, is_volatile)
LibIntrinsics.memmove({{dest}}, {{src}}, {{len}}, {{is_volatile}})
::LibIntrinsics.memmove({{dest}}, {{src}}, {{len}}, {{is_volatile}})
end

macro memset(dest, val, len, is_volatile)
LibIntrinsics.memset({{dest}}, {{val}}, {{len}}, {{is_volatile}})
::LibIntrinsics.memset({{dest}}, {{val}}, {{len}}, {{is_volatile}})
end

def self.read_cycle_counter
Expand Down Expand Up @@ -263,43 +263,43 @@ module Intrinsics
end

macro countleading8(src, zero_is_undef)
LibIntrinsics.countleading8({{src}}, {{zero_is_undef}})
::LibIntrinsics.countleading8({{src}}, {{zero_is_undef}})
end

macro countleading16(src, zero_is_undef)
LibIntrinsics.countleading16({{src}}, {{zero_is_undef}})
::LibIntrinsics.countleading16({{src}}, {{zero_is_undef}})
end

macro countleading32(src, zero_is_undef)
LibIntrinsics.countleading32({{src}}, {{zero_is_undef}})
::LibIntrinsics.countleading32({{src}}, {{zero_is_undef}})
end

macro countleading64(src, zero_is_undef)
LibIntrinsics.countleading64({{src}}, {{zero_is_undef}})
::LibIntrinsics.countleading64({{src}}, {{zero_is_undef}})
end

macro countleading128(src, zero_is_undef)
LibIntrinsics.countleading128({{src}}, {{zero_is_undef}})
::LibIntrinsics.countleading128({{src}}, {{zero_is_undef}})
end

macro counttrailing8(src, zero_is_undef)
LibIntrinsics.counttrailing8({{src}}, {{zero_is_undef}})
::LibIntrinsics.counttrailing8({{src}}, {{zero_is_undef}})
end

macro counttrailing16(src, zero_is_undef)
LibIntrinsics.counttrailing16({{src}}, {{zero_is_undef}})
::LibIntrinsics.counttrailing16({{src}}, {{zero_is_undef}})
end

macro counttrailing32(src, zero_is_undef)
LibIntrinsics.counttrailing32({{src}}, {{zero_is_undef}})
::LibIntrinsics.counttrailing32({{src}}, {{zero_is_undef}})
end

macro counttrailing64(src, zero_is_undef)
LibIntrinsics.counttrailing64({{src}}, {{zero_is_undef}})
::LibIntrinsics.counttrailing64({{src}}, {{zero_is_undef}})
end

macro counttrailing128(src, zero_is_undef)
LibIntrinsics.counttrailing128({{src}}, {{zero_is_undef}})
::LibIntrinsics.counttrailing128({{src}}, {{zero_is_undef}})
end

def self.fshl8(a, b, count) : UInt8
Expand Down Expand Up @@ -343,14 +343,14 @@ module Intrinsics
end

macro va_start(ap)
LibIntrinsics.va_start({{ap}})
::LibIntrinsics.va_start({{ap}})
end

macro va_end(ap)
LibIntrinsics.va_end({{ap}})
::LibIntrinsics.va_end({{ap}})
end
end

macro debugger
Intrinsics.debugtrap
::Intrinsics.debugtrap
end
6 changes: 3 additions & 3 deletions src/json/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ module JSON
private def self.new_from_json_pull_parser(pull : ::JSON::PullParser)
instance = allocate
instance.initialize(__pull_for_json_serializable: pull)
GC.add_finalizer(instance) if instance.responds_to?(:finalize)
::GC.add_finalizer(instance) if instance.responds_to?(:finalize)
instance
end

Expand Down Expand Up @@ -422,8 +422,8 @@ module JSON
# Try to find the discriminator while also getting the raw
# string value of the parsed JSON, so then we can pass it
# to the final type.
json = String.build do |io|
JSON.build(io) do |builder|
json = ::String.build do |io|
::JSON.build(io) do |builder|
builder.start_object
pull.read_object do |key|
if key == {{field.id.stringify}}
Expand Down
8 changes: 4 additions & 4 deletions src/number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct Number
# :nodoc:
macro expand_div(rhs_types, result_type)
{% for rhs in rhs_types %}
@[AlwaysInline]
@[::AlwaysInline]
def /(other : {{rhs}}) : {{result_type}}
{{result_type}}.new(self) / {{result_type}}.new(other)
end
Expand All @@ -84,7 +84,7 @@ struct Number
# [1, 2, 3, 4] of Int64 # : Array(Int64)
# ```
macro [](*nums)
Array({{@type}}).build({{nums.size}}) do |%buffer|
::Array({{@type}}).build({{nums.size}}) do |%buffer|
{% for num, i in nums %}
%buffer[{{i}}] = {{@type}}.new({{num}})
{% end %}
Expand Down Expand Up @@ -113,7 +113,7 @@ struct Number
# Slice[1_i64, 2_i64, 3_i64, 4_i64] # : Slice(Int64)
# ```
macro slice(*nums, read_only = false)
%slice = Slice({{@type}}).new({{nums.size}}, read_only: {{read_only}})
%slice = ::Slice({{@type}}).new({{nums.size}}, read_only: {{read_only}})
{% for num, i in nums %}
%slice.to_unsafe[{{i}}] = {{@type}}.new!({{num}})
{% end %}
Expand All @@ -139,7 +139,7 @@ struct Number
# StaticArray[1_i64, 2_i64, 3_i64, 4_i64] # : StaticArray(Int64)
# ```
macro static_array(*nums)
%array = uninitialized StaticArray({{@type}}, {{nums.size}})
%array = uninitialized ::StaticArray({{@type}}, {{nums.size}})
{% for num, i in nums %}
%array.to_unsafe[{{i}}] = {{@type}}.new!({{num}})
{% end %}
Expand Down
12 changes: 6 additions & 6 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ class Object

def {{method_prefix}}\{{name.var.id}} : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
::raise NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.var.id}} cannot be nil")
::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.var.id}} cannot be nil")
else
value
end
Expand All @@ -574,7 +574,7 @@ class Object

def {{method_prefix}}\{{name.id}}
if (value = {{var_prefix}}\{{name.id}}).nil?
::raise NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.id}} cannot be nil")
::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.id}} cannot be nil")
else
value
end
Expand Down Expand Up @@ -1293,7 +1293,7 @@ class Object
# wrapper.capitalize # => "Hello"
# ```
macro delegate(*methods, to object)
{% if compare_versions(Crystal::VERSION, "1.12.0-dev") >= 0 %}
{% if compare_versions(::Crystal::VERSION, "1.12.0-dev") >= 0 %}
{% eq_operators = %w(<= >= == != []= ===) %}
{% for method in methods %}
{% if method.id.ends_with?('=') && !eq_operators.includes?(method.id.stringify) %}
Expand Down Expand Up @@ -1427,18 +1427,18 @@ class Object
macro def_clone
# Returns a copy of `self` with all instance variables cloned.
def clone
\{% if @type < Reference && !@type.instance_vars.map(&.type).all? { |t| t == ::Bool || t == ::Char || t == ::Symbol || t == ::String || t < ::Number::Primitive } %}
\{% if @type < ::Reference && !@type.instance_vars.map(&.type).all? { |t| t == ::Bool || t == ::Char || t == ::Symbol || t == ::String || t < ::Number::Primitive } %}
exec_recursive_clone do |hash|
clone = \{{@type}}.allocate
hash[object_id] = clone.object_id
clone.initialize_copy(self)
GC.add_finalizer(clone) if clone.responds_to?(:finalize)
::GC.add_finalizer(clone) if clone.responds_to?(:finalize)
clone
end
\{% else %}
clone = \{{@type}}.allocate
clone.initialize_copy(self)
GC.add_finalizer(clone) if clone.responds_to?(:finalize)
::GC.add_finalizer(clone) if clone.responds_to?(:finalize)
clone
\{% end %}
end
Expand Down
6 changes: 3 additions & 3 deletions src/slice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ struct Slice(T)
macro [](*args, read_only = false)
# TODO: there should be a better way to check this, probably
# asking if @type was instantiated or if T is defined
{% if @type.name != "Slice(T)" && T < Number %}
{% if @type.name != "Slice(T)" && T < ::Number %}
{{T}}.slice({{args.splat(", ")}}read_only: {{read_only}})
{% else %}
%ptr = Pointer(typeof({{args.splat}})).malloc({{args.size}})
%ptr = ::Pointer(typeof({{args.splat}})).malloc({{args.size}})
{% for arg, i in args %}
%ptr[{{i}}] = {{arg}}
{% end %}
Slice.new(%ptr, {{args.size}}, read_only: {{read_only}})
::Slice.new(%ptr, {{args.size}}, read_only: {{read_only}})
{% end %}
end

Expand Down
4 changes: 2 additions & 2 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ module Spec
# If the "log" module is required it is configured to emit no entries by default.
def log_setup
defined?(::Log) do
if Log.responds_to?(:setup)
Log.setup_from_env(default_level: :none)
if ::Log.responds_to?(:setup)
::Log.setup_from_env(default_level: :none)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/spec/helpers/iterate.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Spec::Methods
# See `.it_iterates` for details.
macro assert_iterates_yielding(expected, method, *, infinite = false, tuple = false)
%remaining = ({{expected}}).size
%ary = [] of typeof(Enumerable.element_type({{ expected }}))
%ary = [] of typeof(::Enumerable.element_type({{ expected }}))
{{ method.id }} do |{% if tuple %}*{% end %}x|
if %remaining == 0
if {{ infinite }}
Expand All @@ -73,11 +73,11 @@ module Spec::Methods
#
# See `.it_iterates` for details.
macro assert_iterates_iterator(expected, method, *, infinite = false)
%ary = [] of typeof(Enumerable.element_type({{ expected }}))
%ary = [] of typeof(::Enumerable.element_type({{ expected }}))
%iter = {{ method.id }}
({{ expected }}).size.times do
%v = %iter.next
if %v.is_a?(Iterator::Stop)
if %v.is_a?(::Iterator::Stop)
# Compare the actual value directly. Since there are less
# then expected values, the expectation will fail and raise.
%ary.should eq({{ expected }})
Expand All @@ -86,7 +86,7 @@ module Spec::Methods
%ary << %v
end
unless {{ infinite }}
%iter.next.should be_a(Iterator::Stop)
%iter.next.should be_a(::Iterator::Stop)
end

%ary.should eq({{ expected }})
Expand Down
2 changes: 1 addition & 1 deletion src/static_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct StaticArray(T, N)
# * `Number.static_array` is a convenient alternative for designating a
# specific numerical item type.
macro [](*args)
%array = uninitialized StaticArray(typeof({{args.splat}}), {{args.size}})
%array = uninitialized ::StaticArray(typeof({{args.splat}}), {{args.size}})
{% for arg, i in args %}
%array.to_unsafe[{{i}}] = {{arg}}
{% end %}
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/aarch64-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ module Syscall
end

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
@[::AlwaysInline]
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/arm-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ module Syscall
end

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
@[::AlwaysInline]
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/i386-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ module Syscall
end

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
@[::AlwaysInline]
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/x86_64-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ module Syscall
end

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
@[::AlwaysInline]
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

Expand Down
14 changes: 7 additions & 7 deletions src/yaml/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ module YAML
# Define a `new` directly in the included type,
# so it overloads well with other possible initializes

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
def self.new(ctx : ::YAML::ParseContext, node : ::YAML::Nodes::Node)
new_from_yaml_node(ctx, node)
end

private def self.new_from_yaml_node(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
private def self.new_from_yaml_node(ctx : ::YAML::ParseContext, node : ::YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
end
Expand All @@ -170,15 +170,15 @@ module YAML
ctx.record_anchor(node, instance)

instance.initialize(__context_for_yaml_serializable: ctx, __node_for_yaml_serializable: node)
GC.add_finalizer(instance) if instance.responds_to?(:finalize)
::GC.add_finalizer(instance) if instance.responds_to?(:finalize)
instance
end

# When the type is inherited, carry over the `new`
# so it can compete with other possible initializes

macro inherited
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
def self.new(ctx : ::YAML::ParseContext, node : ::YAML::Nodes::Node)
new_from_yaml_node(ctx, node)
end
end
Expand Down Expand Up @@ -409,17 +409,17 @@ module YAML
{% mapping.raise "Mapping argument must be a HashLiteral or a NamedTupleLiteral, not #{mapping.class_name.id}" %}
{% end %}

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
def self.new(ctx : ::YAML::ParseContext, node : ::YAML::Nodes::Node)
ctx.read_alias(node, \{{@type}}) do |obj|
return obj
end

unless node.is_a?(YAML::Nodes::Mapping)
unless node.is_a?(::YAML::Nodes::Mapping)
node.raise "Expected YAML mapping, not #{node.class}"
end

node.each do |key, value|
next unless key.is_a?(YAML::Nodes::Scalar) && value.is_a?(YAML::Nodes::Scalar)
next unless key.is_a?(::YAML::Nodes::Scalar) && value.is_a?(::YAML::Nodes::Scalar)
next unless key.value == {{field.id.stringify}}

discriminator_value = value.value
Expand Down

0 comments on commit 47cd33b

Please sign in to comment.