Skip to content

Commit

Permalink
add --warn-scope command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 28, 2020
1 parent 25ade6d commit a1ca4eb
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Language changes
* Outside of the REPL (e.g. in a file), assigning to a variable within a top-level scope
block is considered ambiguous if a global variable with the same name exists.
A warning is given if that happens, to alert you that the code will work differently
than in the REPL ([#33864]).
than in the REPL.
A new command line option `--warn-scope` controls this warning ([#33864]).

* Converting arbitrary tuples to `NTuple`, e.g. `convert(NTuple, (1, ""))` now gives an error,
where it used to be incorrectly allowed. This is because `NTuple` refers only to homogeneous
Expand Down
1 change: 1 addition & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct JLOptions
outputji::Ptr{UInt8}
output_code_coverage::Ptr{UInt8}
incremental::Int8
warn_scope::Int8
end

# This runs early in the sysimage != is not defined yet
Expand Down
1 change: 1 addition & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ static void jl_init_ast_ctx(jl_ast_context_t *ast_ctx) JL_NOTSAFEPOINT
ctx->task = NULL;
ctx->module = NULL;
set(symbol(fl_ctx, "*depwarn-opt*"), fixnum(jl_options.depwarn));
set(symbol(fl_ctx, "*scopewarn-opt*"), fixnum(jl_options.warn_scope));
}

// There should be no GC allocation while holding this lock
Expand Down
2 changes: 2 additions & 0 deletions src/jlfrontend.scm
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@
(if (equal? instead "") ""
(string #\newline "Use `" instead "` instead."))))

(define *scopewarn-opt* 1)

; Corresponds to --depwarn 0="no", 1="yes", 2="error"
(define *depwarn-opt* 1)

Expand Down
18 changes: 15 additions & 3 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ jl_options_t jl_options = { 0, // quiet
NULL, // output-ji
NULL, // output-code_coverage
0, // incremental
0 // image_file_specified
0, // image_file_specified
JL_OPTIONS_WARN_SCOPE_ON // ambiguous scope warning
};

static const char usage[] = "julia [switches] -- [programfile] [args...]\n";
Expand Down Expand Up @@ -109,7 +110,8 @@ static const char opts[] =

// error and warning options
" --depwarn={yes|no|error} Enable or disable syntax and method deprecation warnings (\"error\" turns warnings into errors)\n"
" --warn-overwrite={yes|no} Enable or disable method overwrite warnings\n\n"
" --warn-overwrite={yes|no} Enable or disable method overwrite warnings\n"
" --warn-scope={yes|no} Enable or disable warning for ambiguous top-level scope\n\n"

// code generation options
" -C, --cpu-target <target> Limit usage of CPU features up to <target>; set to \"help\" to see the available options\n"
Expand Down Expand Up @@ -169,6 +171,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
opt_output_bc,
opt_depwarn,
opt_warn_overwrite,
opt_warn_scope,
opt_inline,
opt_polly,
opt_trace_compile,
Expand Down Expand Up @@ -225,6 +228,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
{ "output-incremental",required_argument, 0, opt_incremental },
{ "depwarn", required_argument, 0, opt_depwarn },
{ "warn-overwrite", required_argument, 0, opt_warn_overwrite },
{ "warn-scope", required_argument, 0, opt_warn_scope },
{ "inline", required_argument, 0, opt_inline },
{ "polly", required_argument, 0, opt_polly },
{ "trace-compile", required_argument, 0, opt_trace_compile },
Expand Down Expand Up @@ -554,7 +558,15 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
else if (!strcmp(optarg,"no"))
jl_options.warn_overwrite = JL_OPTIONS_WARN_OVERWRITE_OFF;
else
jl_errorf("julia: invalid argument to --warn-overwrite={yes|no|} (%s)", optarg);
jl_errorf("julia: invalid argument to --warn-overwrite={yes|no} (%s)", optarg);
break;
case opt_warn_scope:
if (!strcmp(optarg,"yes"))
jl_options.warn_scope = JL_OPTIONS_WARN_SCOPE_ON;
else if (!strcmp(optarg,"no"))
jl_options.warn_scope = JL_OPTIONS_WARN_SCOPE_OFF;
else
jl_errorf("julia: invalid argument to --warn-scope={yes|no} (%s)", optarg);
break;
case opt_inline:
if (!strcmp(optarg,"yes"))
Expand Down
3 changes: 2 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,8 @@
(else
(if (and (eq? (car e) '=) (symbol? (cadr e))
scope (null? (lam:vars (scope:lam scope)))
(warn-var?! (cadr e) scope))
(warn-var?! (cadr e) scope)
(= *scopewarn-opt* 1))
(let* ((v (cadr e))
(loc (extract-line-file loc))
(line (if (= (car loc) 0) (julia-current-line) (car loc)))
Expand Down
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,7 @@ typedef struct {
const char *output_code_coverage;
int8_t incremental;
int8_t image_file_specified;
int8_t warn_scope;
} jl_options_t;

extern JL_DLLEXPORT jl_options_t jl_options;
Expand Down Expand Up @@ -1992,6 +1993,9 @@ JL_DLLEXPORT int jl_generating_output(void) JL_NOTSAFEPOINT;
#define JL_OPTIONS_WARN_OVERWRITE_OFF 0
#define JL_OPTIONS_WARN_OVERWRITE_ON 1

#define JL_OPTIONS_WARN_SCOPE_OFF 0
#define JL_OPTIONS_WARN_SCOPE_ON 1

#define JL_OPTIONS_POLLY_ON 1
#define JL_OPTIONS_POLLY_OFF 0

Expand Down

0 comments on commit a1ca4eb

Please sign in to comment.