Skip to content

Commit cfec173

Browse files
authored
add warning for function declaration with undefined static parameter (#46608)
Refs #27813 (does not fix it)
1 parent c3b6757 commit cfec173

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/method.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -957,12 +957,6 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
957957
size_t i, na = jl_svec_len(atypes);
958958

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

967961
jl_methtable_t *external_mt = mt;
968962
if (!mt)
@@ -972,6 +966,12 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
972966
if (mt->frozen)
973967
jl_error("cannot add methods to a builtin function");
974968

969+
assert(jl_is_linenode(functionloc));
970+
jl_sym_t *file = (jl_sym_t*)jl_linenode_file(functionloc);
971+
if (!jl_is_symbol(file))
972+
file = jl_empty_sym;
973+
int32_t line = jl_linenode_line(functionloc);
974+
975975
// TODO: derive our debug name from the syntax instead of the type
976976
name = mt->name;
977977
if (mt == jl_type_type_mt || mt == jl_nonfunction_mt || external_mt) {
@@ -988,6 +988,29 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
988988
}
989989
}
990990
}
991+
992+
for (i = jl_svec_len(tvars); i > 0; i--) {
993+
jl_value_t *tv = jl_svecref(tvars, i - 1);
994+
if (!jl_is_typevar(tv))
995+
jl_type_error("method signature", (jl_value_t*)jl_tvar_type, tv);
996+
if (!jl_has_typevar(argtype, (jl_tvar_t*)tv)) // deprecate this to an error in v2
997+
jl_printf(JL_STDERR,
998+
"WARNING: method definition for %s at %s:%d declares type variable %s but does not use it.\n",
999+
jl_symbol_name(name),
1000+
jl_symbol_name(file),
1001+
line,
1002+
jl_symbol_name(((jl_tvar_t*)tv)->name));
1003+
argtype = jl_new_struct(jl_unionall_type, tv, argtype);
1004+
}
1005+
if (jl_has_free_typevars(argtype)) {
1006+
jl_exceptionf(jl_argumenterror_type,
1007+
"method definition for %s at %s:%d has free type variables",
1008+
jl_symbol_name(name),
1009+
jl_symbol_name(file),
1010+
line);
1011+
}
1012+
1013+
9911014
if (!jl_is_code_info(f)) {
9921015
// this occurs when there is a closure being added to an out-of-scope function
9931016
// the user should only do this at the toplevel
@@ -1002,20 +1025,10 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
10021025
m->name = name;
10031026
m->isva = isva;
10041027
m->nargs = nargs;
1005-
assert(jl_is_linenode(functionloc));
1006-
jl_value_t *file = jl_linenode_file(functionloc);
1007-
m->file = jl_is_symbol(file) ? (jl_sym_t*)file : jl_empty_sym;
1008-
m->line = jl_linenode_line(functionloc);
1028+
m->file = file;
1029+
m->line = line;
10091030
jl_method_set_source(m, f);
10101031

1011-
if (jl_has_free_typevars(argtype)) {
1012-
jl_exceptionf(jl_argumenterror_type,
1013-
"method definition for %s at %s:%d has free type variables",
1014-
jl_symbol_name(name),
1015-
jl_symbol_name(m->file),
1016-
m->line);
1017-
}
1018-
10191032
for (i = 0; i < na; i++) {
10201033
jl_value_t *elt = jl_svecref(atypes, i);
10211034
if (!jl_is_type(elt) && !jl_is_typevar(elt) && !jl_is_vararg(elt)) {
@@ -1025,22 +1038,22 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
10251038
"invalid type for argument number %d in method definition for %s at %s:%d",
10261039
i,
10271040
jl_symbol_name(name),
1028-
jl_symbol_name(m->file),
1029-
m->line);
1041+
jl_symbol_name(file),
1042+
line);
10301043
else
10311044
jl_exceptionf(jl_argumenterror_type,
10321045
"invalid type for argument %s in method definition for %s at %s:%d",
10331046
jl_symbol_name(argname),
10341047
jl_symbol_name(name),
1035-
jl_symbol_name(m->file),
1036-
m->line);
1048+
jl_symbol_name(file),
1049+
line);
10371050
}
10381051
if (jl_is_vararg(elt) && i < na-1)
10391052
jl_exceptionf(jl_argumenterror_type,
10401053
"Vararg on non-final argument in method definition for %s at %s:%d",
10411054
jl_symbol_name(name),
1042-
jl_symbol_name(m->file),
1043-
m->line);
1055+
jl_symbol_name(file),
1056+
line);
10441057
}
10451058

10461059
#ifdef RECORD_METHOD_ORDER

stdlib/LinearAlgebra/src/bunchkaufman.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ BunchKaufman(A::AbstractMatrix{T}, ipiv::AbstractVector{<:Integer}, uplo::Abstra
8080
symmetric::Bool, rook::Bool, info::BlasInt) where {T} =
8181
BunchKaufman{T,typeof(A),typeof(ipiv)}(A, ipiv, uplo, symmetric, rook, info)
8282
# backwards-compatible constructors (remove with Julia 2.0)
83-
@deprecate(BunchKaufman(LD, ipiv, uplo, symmetric, rook, info) where {T,S},
83+
@deprecate(BunchKaufman{T,S}(LD, ipiv, uplo, symmetric, rook, info) where {T,S},
8484
BunchKaufman{T,S,typeof(ipiv)}(LD, ipiv, uplo, symmetric, rook, info), false)
8585

8686
# iteration for destructuring into components

0 commit comments

Comments
 (0)