Skip to content

Commit 23845b5

Browse files
committed
Print source-information when encountering ambiguous method during compilation
1 parent bc6bc4d commit 23845b5

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/codegen.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ void *jl_get_llvmf(jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
10871087
jl_lambda_info_t *linfo = NULL;
10881088
JL_GC_PUSH2(&linfo, &tt);
10891089
if (tt != NULL) {
1090-
linfo = jl_get_specialization1(tt);
1090+
int8_t ambig;
1091+
linfo = jl_get_specialization1(tt, &ambig);
10911092
if (linfo == NULL) {
10921093
linfo = jl_method_lookup_by_type(
10931094
((jl_datatype_t*)jl_tparam0(tt))->name->mt, tt, 0, 0);
@@ -2729,7 +2730,11 @@ static jl_cgval_t emit_call(jl_expr_t *ex, jl_codectx_t *ctx)
27292730
jl_sprint(args[0]),
27302731
jl_sprint((jl_value_t*)aty));
27312732
}*/
2732-
jl_lambda_info_t *li = jl_get_specialization1((jl_tupletype_t*)aty);
2733+
int8_t ambig;
2734+
jl_lambda_info_t *li = jl_get_specialization1((jl_tupletype_t*)aty, &ambig);
2735+
if (ambig) {
2736+
jl_printf(JL_STDERR, "Detected ambiguity while compiling %s\n", ctx->name);
2737+
}
27332738
if (li != NULL) {
27342739
assert(li->functionObjectsDecls.functionObject != NULL);
27352740
theFptr = (Value*)li->functionObjectsDecls.functionObject;
@@ -3506,7 +3511,8 @@ static Function *gen_cfun_wrapper(jl_function_t *ff, jl_value_t *jlrettype, jl_t
35063511
jl_error("va_arg syntax not allowed for cfunction argument list");
35073512

35083513
const char *name = "cfunction";
3509-
jl_lambda_info_t *lam = jl_get_specialization1((jl_tupletype_t*)sigt);
3514+
int8_t ambig;
3515+
jl_lambda_info_t *lam = jl_get_specialization1((jl_tupletype_t*)sigt, &ambig);
35103516
jl_value_t *astrt = (jl_value_t*)jl_any_type;
35113517
if (lam != NULL) {
35123518
name = jl_symbol_name(lam->def->name);

src/gf.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@ void print_func_loc(JL_STREAM *s, jl_method_t *m)
717717
To check this, jl_types_equal_generic needs to be more sophisticated
718718
so (T,T) is not equivalent to (Any,Any). (TODO)
719719
*/
720+
721+
int8_t issued_ambig_warn;
722+
720723
static void issue_ambig_warn(jl_typemap_entry_t *m)
721724
{
722725
JL_STREAM *s = JL_STDERR;
@@ -729,6 +732,7 @@ static void issue_ambig_warn(jl_typemap_entry_t *m)
729732
print_func_loc(s, ambigm->func.method);
730733
jl_printf(s, "\nSee the documentation for tips on eliminating ambiguities.\n");
731734
m->ambig = ambigm->ambig = (jl_typemap_entry_t*) jl_nothing;
735+
issued_ambig_warn = 1;
732736
}
733737

734738
struct ambiguous_matches_env {
@@ -990,12 +994,13 @@ jl_lambda_info_t *jl_method_lookup(jl_methtable_t *mt, jl_value_t **args, size_t
990994
JL_DLLEXPORT jl_value_t *jl_matching_methods(jl_tupletype_t *types, int lim);
991995

992996
// compile-time method lookup
993-
jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types)
997+
jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types, int8_t *ambig)
994998
{
995999
assert(jl_nparams(types) > 0);
9961000
if (!jl_is_leaf_type((jl_value_t*)types))
9971001
return NULL;
9981002
assert(jl_is_datatype(jl_tparam0(types)));
1003+
issued_ambig_warn = 0;
9991004

10001005
// make sure exactly 1 method matches (issue #7302).
10011006
int i;
@@ -1006,6 +1011,7 @@ jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types)
10061011
// to analyze them in detail.
10071012
if (ti == (jl_value_t*)jl_datatype_type || jl_is_tuple_type(ti)) {
10081013
jl_value_t *matches = jl_matching_methods(types, 1);
1014+
*ambig = issued_ambig_warn;
10091015
if (matches == jl_false)
10101016
return NULL;
10111017
break;
@@ -1022,6 +1028,7 @@ jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types)
10221028
} JL_CATCH {
10231029
goto not_found;
10241030
}
1031+
*ambig = issued_ambig_warn;
10251032
if (sf == NULL || sf->code == NULL || sf->inInference)
10261033
goto not_found;
10271034
if (sf->functionObjectsDecls.functionObject == NULL) {
@@ -1038,7 +1045,8 @@ jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types)
10381045

10391046
JL_DLLEXPORT void jl_compile_hint(jl_tupletype_t *types)
10401047
{
1041-
(void)jl_get_specialization1(types);
1048+
int8_t ambig;
1049+
(void)jl_get_specialization1(types, &ambig);
10421050
}
10431051

10441052
// add type of `f` to front of argument tuple type
@@ -1076,6 +1084,7 @@ static int _compile_all_tvar_union(jl_tupletype_t *methsig, jl_svec_t *tvars)
10761084
// and expanding the Union may give a leaf function
10771085
jl_tvar_t **tvs;
10781086
int tvarslen;
1087+
int8_t ambig;
10791088
if (jl_is_typevar(tvars)) {
10801089
tvs = (jl_tvar_t**)&tvars;
10811090
tvarslen = 1;
@@ -1087,7 +1096,7 @@ static int _compile_all_tvar_union(jl_tupletype_t *methsig, jl_svec_t *tvars)
10871096
if (jl_is_leaf_type((jl_value_t*)methsig)) {
10881097
// usually can create a specialized version of the function,
10891098
// if the signature is already a leaftype
1090-
jl_lambda_info_t *spec = jl_get_specialization1(methsig);
1099+
jl_lambda_info_t *spec = jl_get_specialization1(methsig, &ambig);
10911100
if (spec)
10921101
return 1;
10931102
}
@@ -1120,7 +1129,7 @@ static int _compile_all_tvar_union(jl_tupletype_t *methsig, jl_svec_t *tvars)
11201129
goto getnext; // signature wouldn't be callable / is invalid -- skip it
11211130
}
11221131
if (jl_is_leaf_type(sig)) {
1123-
if (jl_get_specialization1((jl_tupletype_t*)sig)) {
1132+
if (jl_get_specialization1((jl_tupletype_t*)sig, &ambig)) {
11241133
if (!jl_has_typevars((jl_value_t*)sig)) goto getnext; // success
11251134
}
11261135
}

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ int32_t jl_get_llvm_gv(jl_value_t *p);
281281
void jl_idtable_rehash(jl_array_t **pa, size_t newsz);
282282

283283
JL_DLLEXPORT jl_methtable_t *jl_new_method_table(jl_sym_t *name, jl_module_t *module);
284-
jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types);
284+
jl_lambda_info_t *jl_get_specialization1(jl_tupletype_t *types, int8_t *ambig);
285285
jl_function_t *jl_module_get_initializer(jl_module_t *m);
286286
uint32_t jl_module_next_counter(jl_module_t *m);
287287
void jl_fptr_to_llvm(jl_fptr_t fptr, jl_lambda_info_t *lam, int specsig);

src/threading.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ JL_DLLEXPORT jl_value_t *jl_threading_run(jl_svec_t *args)
398398
int8_t gc_state = jl_gc_unsafe_enter();
399399
JL_GC_PUSH1(&argtypes);
400400
argtypes = arg_type_tuple(jl_svec_data(args), jl_svec_len(args));
401-
jl_lambda_info_t *li = jl_get_specialization1(argtypes);
401+
int8_t ambig;
402+
jl_lambda_info_t *li = jl_get_specialization1(argtypes, &ambig);
402403
jl_generate_fptr(li);
403404

404405
threadwork.command = TI_THREADWORK_RUN;

src/toplevel.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ static void jl_module_load_time_initialize(jl_module_t *m)
8181
jl_value_t *tt = jl_is_type(f) ? (jl_value_t*)jl_wrap_Type(f) : jl_typeof(f);
8282
JL_GC_PUSH1(&tt);
8383
tt = (jl_value_t*)jl_apply_tuple_type_v(&tt, 1);
84-
jl_get_specialization1((jl_tupletype_t*)tt);
84+
int8_t ambig;
85+
jl_get_specialization1((jl_tupletype_t*)tt, &ambig);
8586
JL_GC_POP();
8687
}
8788
}

0 commit comments

Comments
 (0)