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

RFC: remove default importall Base.Operators #12235

Merged
merged 4 commits into from
Jul 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ Language changes
* `global x` in a nested scope is now a syntax error if `x` is local
to the enclosing scope ([#7264]/[#11985]).

* The default `importall Base.Operators` is deprecated, and relying on it
will give a warning ([#8113]).

Command line option changes
---------------------------

Expand Down Expand Up @@ -1420,6 +1423,7 @@ Too numerous to mention.
[#7992]: https://github.com/JuliaLang/julia/issues/7992
[#8011]: https://github.com/JuliaLang/julia/issues/8011
[#8089]: https://github.com/JuliaLang/julia/issues/8089
[#8113]: https://github.com/JuliaLang/julia/issues/8113
[#8135]: https://github.com/JuliaLang/julia/issues/8135
[#8152]: https://github.com/JuliaLang/julia/issues/8152
[#8246]: https://github.com/JuliaLang/julia/issues/8246
Expand Down Expand Up @@ -1535,4 +1539,6 @@ Too numerous to mention.
[#11922]: https://github.com/JuliaLang/julia/issues/11922
[#11985]: https://github.com/JuliaLang/julia/issues/11985
[#12031]: https://github.com/JuliaLang/julia/issues/12031
[#12034]: https://github.com/JuliaLang/julia/issues/12034
[#12087]: https://github.com/JuliaLang/julia/issues/12087
[#12137]: https://github.com/JuliaLang/julia/issues/12137
2 changes: 2 additions & 0 deletions base/Dates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Dates

importall ..Base.Operators

include("dates/types.jl")
include("dates/periods.jl")
include("dates/accessors.jl")
Expand Down
3 changes: 2 additions & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import Base:
Display,
display,
writemime,
AnyDict
AnyDict,
==

import ..LineEdit:
CompletionProvider,
Expand Down
3 changes: 1 addition & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ using ..Cartesian
import Base.promote_eltype
import Base.@get!
import Base.num_bit_chunks, Base._msk_end, Base.unsafe_bitgetindex
import Base.(.+), Base.(.-), Base.(.*), Base.(./), Base.(.\), Base.(.//)
import Base.(.==), Base.(.<), Base.(.!=), Base.(.<=)
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, .%, .<<, .>>, .^
export broadcast, broadcast!, broadcast_function, broadcast!_function, bitbroadcast
export broadcast_getindex, broadcast_setindex!

Expand Down
2 changes: 2 additions & 0 deletions base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Grisu

importall ..Base.Operators

export print_shortest
export DIGITS, grisu

Expand Down
2 changes: 2 additions & 0 deletions base/grisu/bignums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

module Bignums

import Base: ==, <

export Bignum

const kMaxSignificantBits = 3584
Expand Down
1 change: 1 addition & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module LinAlg

importall Base
importall ..Base.Operators
import Base: USE_BLAS64, size, copy, copy_transpose!, power_by_squaring,
print_matrix, transpose!, unsafe_getindex, unsafe_setindex!

Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Markdown

import Base: writemime
import Base: writemime, ==

typealias String AbstractString

Expand Down
1 change: 1 addition & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module IteratorsMD

import Base: eltype, length, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex, ndims
importall ..Base.Operators
import Base: simd_outer_range, simd_inner_length, simd_index, @generated
import Base: @nref, @ncall, @nif, @nexprs, LinearFast, LinearSlow, to_index

Expand Down
2 changes: 2 additions & 0 deletions base/pkg/reqs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Reqs

import Base: ==

using ..Types

# representing lines of REQUIRE files
Expand Down
1 change: 1 addition & 0 deletions base/pkg/resolve/fieldvalue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module FieldValues

using ...VersionWeights
importall .....Base.Operators

export FieldValue, Field, validmax, secondmax

Expand Down
2 changes: 2 additions & 0 deletions base/pkg/resolve/versionweight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module VersionWeights

importall ....Base.Operators

export VersionWeight

immutable HierarchicalValue{T}
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Types

export VersionInterval, VersionSet, Requires, Available, Fixed, merge_requires!, satisfies
import Base: show, isempty, in, intersect, hash, deepcopy_internal
import Base: show, isempty, in, intersect, hash, deepcopy_internal, ==

immutable VersionInterval
lower::VersionNumber
Expand Down
1 change: 1 addition & 0 deletions base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Base.Sort: Forward
using Base.LinAlg: AbstractTriangular

importall Base
importall ..Base.Operators
importall Base.LinAlg
import Base.promote_eltype
import Base.@get!
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CHOLMOD

import Base: (*), convert, copy, eltype, getindex, show, showarray, size,
linearindexing, LinearFast, LinearSlow
linearindexing, LinearFast, LinearSlow, ctranspose

import Base.LinAlg: (\), A_mul_Bc, A_mul_Bt, Ac_ldiv_B, Ac_mul_B, At_ldiv_B, At_mul_B,
cholfact, det, diag, ishermitian, isposdef,
Expand Down
2 changes: 2 additions & 0 deletions base/sparse/spqr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module SPQR

import Base: \

# ordering options */
const ORDERING_FIXED = Int32(0)
const ORDERING_NATURAL = Int32(1)
Expand Down
13 changes: 6 additions & 7 deletions doc/manual/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,20 @@ contain ``using Base``, since this is needed in the vast majority of cases.
Default top-level definitions and bare modules
----------------------------------------------

In addition to ``using Base``, all operators are explicitly imported,
since one typically wants to extend operators rather than creating entirely
new definitions of them. A module also automatically contains a definition
of the ``eval`` function, which evaluates expressions within the context of
that module.
In addition to ``using Base``, modules also perform ``import Base.call`` by
default, to facilitate adding constructors to new types.
A new module also automatically contains a definition of the ``eval`` function,
which evaluates expressions within the context of that module.

If these ``Base`` definitions are not wanted, modules can be defined using the
If these default definitions are not wanted, modules can be defined using the
keyword ``baremodule`` instead (note: ``Core`` is still imported, as per above).
In terms of ``baremodule``, a standard ``module`` looks like this::

baremodule Mod

using Base

importall Base.Operators
import Base.call

eval(x) = Core.eval(Mod, x)
eval(m,x) = Core.eval(m, x)
Expand Down
2 changes: 2 additions & 0 deletions examples/modint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module ModInts
export ModInt

import Base: +, -, *

immutable ModInt{n} <: Integer
k::Int
ModInt(k) = new(mod(k,n))
Expand Down
2 changes: 2 additions & 0 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ static void jl_serialize_module(ios_t *s, jl_module_t *m)
}
jl_serialize_value(s, m->constant_table);
write_uint8(s, m->istopmod);
write_uint8(s, m->std_imports);
write_uint64(s, m->uuid);
}

Expand Down Expand Up @@ -1336,6 +1337,7 @@ static jl_value_t *jl_deserialize_value_(ios_t *s, jl_value_t *vtag, jl_value_t
m->constant_table = (jl_array_t*)jl_deserialize_value(s, (jl_value_t**)&m->constant_table);
if (m->constant_table != NULL) jl_gc_wb(m, m->constant_table);
m->istopmod = read_uint8(s);
m->std_imports = read_uint8(s);
m->uuid = read_uint64(s);
return (jl_value_t*)m;
}
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ typedef struct _jl_module_t {
jl_array_t *constant_table;
jl_function_t *call_func; // cached lookup of `call` within this module
uint8_t istopmod;
uint8_t std_imports; // only for temporarily deprecating `importall Base.Operators`
uint64_t uuid;
} jl_module_t;

Expand Down
20 changes: 20 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jl_module_t *jl_new_module(jl_sym_t *name)
m->constant_table = NULL;
m->call_func = NULL;
m->istopmod = 0;
m->std_imports = 0;
m->uuid = uv_now(uv_default_loop());
htable_new(&m->bindings, 0);
arraylist_new(&m->usings, 0);
Expand Down Expand Up @@ -120,6 +121,16 @@ DLLEXPORT jl_module_t *jl_get_module_of_binding(jl_module_t *m, jl_sym_t *var)
// and overwriting.
DLLEXPORT jl_binding_t *jl_get_binding_for_method_def(jl_module_t *m, jl_sym_t *var)
{
if (jl_base_module && m->std_imports && !jl_binding_resolved_p(m,var)) {
jl_module_t *opmod = (jl_module_t*)jl_get_global(jl_base_module, jl_symbol("Operators"));
if (opmod != NULL && jl_defines_or_exports_p(opmod, var)) {
jl_printf(JL_STDERR,
"WARNING: module %s should explicitly import %s from %s\n",
m->name->name, var->name, jl_base_module->name->name);
jl_module_import(m, opmod, var);
}
}

jl_binding_t **bp = (jl_binding_t**)ptrhash_bp(&m->bindings, var);
jl_binding_t *b = *bp;

Expand All @@ -133,6 +144,15 @@ DLLEXPORT jl_binding_t *jl_get_binding_for_method_def(jl_module_t *m, jl_sym_t *
jl_errorf("error in method definition: %s.%s cannot be extended", b->owner->name->name, var->name);
}
else {
if (jl_base_module && m->std_imports && b->owner == jl_base_module) {
jl_module_t *opmod = (jl_module_t*)jl_get_global(jl_base_module, jl_symbol("Operators"));
if (opmod != NULL && jl_defines_or_exports_p(opmod, var)) {
jl_printf(JL_STDERR,
"WARNING: module %s should explicitly import %s from %s\n",
m->name->name, var->name, b->owner->name->name);
return b2;
}
}
jl_errorf("error in method definition: function %s.%s must be explicitly imported to be extended", b->owner->name->name, var->name);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ void jl_add_standard_imports(jl_module_t *m)
assert(jl_base_module != NULL);
// using Base
jl_module_using(m, jl_base_module);
// importall Base.Operators
jl_module_t *opmod = (jl_module_t*)jl_get_global(jl_base_module, jl_symbol("Operators"));
if (opmod != NULL)
jl_module_importall(m, opmod);
// import Base.call
jl_module_import(m, jl_base_module, jl_symbol("call"));
m->std_imports = 1;
}

jl_module_t *jl_new_main_module(void)
Expand Down
1 change: 1 addition & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ test5536(a::Union{Real, AbstractArray}) = "Non-splatting"
@test_throws LoadError include_string("#= #= #= =# =# =")

# issue #6142
import Base: +
type A6142 <: AbstractMatrix{Float64}; end
+{TJ}(x::A6142, y::UniformScaling{TJ}) = "UniformScaling method called"
+(x::A6142, y::AbstractArray) = "AbstractArray method called"
Expand Down
1 change: 1 addition & 0 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ end
size(Phi::CPM)=(size(Phi.kraus,1)^2,size(Phi.kraus,3)^2)
issym(Phi::CPM)=false
ishermitian(Phi::CPM)=false
import Base: *
function *{T<:Base.LinAlg.BlasFloat}(Phi::CPM{T},rho::Vector{T})
rho=reshape(rho,(size(Phi.kraus,3),size(Phi.kraus,3)))
rho2=zeros(T,(size(Phi.kraus,1),size(Phi.kraus,1)))
Expand Down