Skip to content

Commit

Permalink
add warning for function declaration with undefined static parameter (#…
Browse files Browse the repository at this point in the history
…46608)

Refs #27813 (does not fix it)

(cherry picked from commit cfec173)
  • Loading branch information
vtjnash authored and KristofferC committed Sep 7, 2022
1 parent 056e260 commit 3761fd3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
61 changes: 37 additions & 24 deletions src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,6 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
size_t i, na = jl_svec_len(atypes);

argtype = (jl_value_t*)jl_apply_tuple_type(atypes);
for (i = jl_svec_len(tvars); i > 0; i--) {
jl_value_t *tv = jl_svecref(tvars, i - 1);
if (!jl_is_typevar(tv))
jl_type_error("method signature", (jl_value_t*)jl_tvar_type, tv);
argtype = jl_new_struct(jl_unionall_type, tv, argtype);
}

jl_methtable_t *external_mt = mt;
if (!mt)
Expand All @@ -921,6 +915,12 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
if (mt->frozen)
jl_error("cannot add methods to a builtin function");

assert(jl_is_linenode(functionloc));
jl_sym_t *file = (jl_sym_t*)jl_linenode_file(functionloc);
if (!jl_is_symbol(file))
file = jl_empty_sym;
int32_t line = jl_linenode_line(functionloc);

// TODO: derive our debug name from the syntax instead of the type
name = mt->name;
if (mt == jl_type_type_mt || mt == jl_nonfunction_mt || external_mt) {
Expand All @@ -937,6 +937,29 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
}
}
}

for (i = jl_svec_len(tvars); i > 0; i--) {
jl_value_t *tv = jl_svecref(tvars, i - 1);
if (!jl_is_typevar(tv))
jl_type_error("method signature", (jl_value_t*)jl_tvar_type, tv);
if (!jl_has_typevar(argtype, (jl_tvar_t*)tv)) // deprecate this to an error in v2
jl_printf(JL_STDERR,
"WARNING: method definition for %s at %s:%d declares type variable %s but does not use it.\n",
jl_symbol_name(name),
jl_symbol_name(file),
line,
jl_symbol_name(((jl_tvar_t*)tv)->name));
argtype = jl_new_struct(jl_unionall_type, tv, argtype);
}
if (jl_has_free_typevars(argtype)) {
jl_exceptionf(jl_argumenterror_type,
"method definition for %s at %s:%d has free type variables",
jl_symbol_name(name),
jl_symbol_name(file),
line);
}


if (!jl_is_code_info(f)) {
// this occurs when there is a closure being added to an out-of-scope function
// the user should only do this at the toplevel
Expand All @@ -951,20 +974,10 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
m->name = name;
m->isva = isva;
m->nargs = nargs;
assert(jl_is_linenode(functionloc));
jl_value_t *file = jl_linenode_file(functionloc);
m->file = jl_is_symbol(file) ? (jl_sym_t*)file : jl_empty_sym;
m->line = jl_linenode_line(functionloc);
m->file = file;
m->line = line;
jl_method_set_source(m, f);

if (jl_has_free_typevars(argtype)) {
jl_exceptionf(jl_argumenterror_type,
"method definition for %s at %s:%d has free type variables",
jl_symbol_name(name),
jl_symbol_name(m->file),
m->line);
}

for (i = 0; i < na; i++) {
jl_value_t *elt = jl_svecref(atypes, i);
if (!jl_is_type(elt) && !jl_is_typevar(elt) && !jl_is_vararg(elt)) {
Expand All @@ -974,22 +987,22 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
"invalid type for argument number %d in method definition for %s at %s:%d",
i,
jl_symbol_name(name),
jl_symbol_name(m->file),
m->line);
jl_symbol_name(file),
line);
else
jl_exceptionf(jl_argumenterror_type,
"invalid type for argument %s in method definition for %s at %s:%d",
jl_symbol_name(argname),
jl_symbol_name(name),
jl_symbol_name(m->file),
m->line);
jl_symbol_name(file),
line);
}
if (jl_is_vararg(elt) && i < na-1)
jl_exceptionf(jl_argumenterror_type,
"Vararg on non-final argument in method definition for %s at %s:%d",
jl_symbol_name(name),
jl_symbol_name(m->file),
m->line);
jl_symbol_name(file),
line);
}

#ifdef RECORD_METHOD_ORDER
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/bunchkaufman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ BunchKaufman(A::AbstractMatrix{T}, ipiv::AbstractVector{<:Integer}, uplo::Abstra
symmetric::Bool, rook::Bool, info::BlasInt) where {T} =
BunchKaufman{T,typeof(A),typeof(ipiv)}(A, ipiv, uplo, symmetric, rook, info)
# backwards-compatible constructors (remove with Julia 2.0)
@deprecate(BunchKaufman(LD, ipiv, uplo, symmetric, rook, info) where {T,S},
@deprecate(BunchKaufman{T,S}(LD, ipiv, uplo, symmetric, rook, info) where {T,S},
BunchKaufman{T,S,typeof(ipiv)}(LD, ipiv, uplo, symmetric, rook, info), false)

# iteration for destructuring into components
Expand Down

0 comments on commit 3761fd3

Please sign in to comment.