Skip to content

Commit ebf1918

Browse files
committed
Provide a better error when no substs is provided for generic param
1 parent d0dc1e3 commit ebf1918

File tree

1 file changed

+39
-26
lines changed
  • src/librustc_typeck/check

1 file changed

+39
-26
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,13 @@ fn typeck_tables_of_with_fallback<'tcx>(
12481248
"Type variable {:?} is equal to diverging var {:?}",
12491249
var, diverging_var
12501250
);
1251-
best_var = Some(var);
1252-
best_diverging_var = Some(diverging_var);
1251+
1252+
debug!(
1253+
"Var origin: {:?}",
1254+
fcx.infcx.type_variables.borrow().var_origin(*vid1)
1255+
);
1256+
best_var = Some(vid1);
1257+
best_diverging_var = Some(vid2);
12531258
}
12541259
}
12551260
_ => bug!(
@@ -1261,31 +1266,39 @@ fn typeck_tables_of_with_fallback<'tcx>(
12611266
}
12621267
}
12631268

1264-
let (var_span, diverging_var_span) =
1265-
match (&best_var.unwrap().kind, &best_diverging_var.unwrap().kind) {
1266-
(
1267-
ty::Infer(ty::InferTy::TyVar(var_vid)),
1268-
ty::Infer(ty::InferTy::TyVar(diverging_var_vid)),
1269-
) => (
1270-
fcx.infcx.type_variables.borrow().var_origin(*var_vid).span,
1271-
fcx.infcx
1272-
.type_variables
1273-
.borrow()
1274-
.var_origin(*diverging_var_vid)
1275-
.span,
1276-
),
1277-
_ => bug!("Type is not a ty variable: {:?}", best_var),
1278-
};
1269+
let var_origin =
1270+
*fcx.infcx.type_variables.borrow().var_origin(*best_var.unwrap());
1271+
let diverging_var_span = fcx
1272+
.infcx
1273+
.type_variables
1274+
.borrow()
1275+
.var_origin(*best_diverging_var.unwrap())
1276+
.span;
1277+
1278+
let mut err = fcx.tcx().sess.struct_span_warn(
1279+
path.span,
1280+
"Fallback to `!` may introduce undefined behavior",
1281+
);
12791282

1280-
fcx.tcx()
1281-
.sess
1282-
.struct_span_warn(
1283-
path.span,
1284-
"Fallback to `!` may introduce undefined behavior",
1285-
)
1286-
.span_note(var_span, "the type here was inferred to `!`")
1287-
.span_note(diverging_var_span, "... due to this expression")
1288-
.emit();
1283+
match var_origin.kind {
1284+
TypeVariableOriginKind::TypeParameterDefinition(name, did) => {
1285+
err.span_note(
1286+
var_origin.span,
1287+
&format!("the type parameter {} here was inferred to `!`", name),
1288+
);
1289+
if let Some(did) = did {
1290+
err.span_note(
1291+
fcx.tcx.def_span(did),
1292+
"(type parameter defined here)",
1293+
);
1294+
}
1295+
}
1296+
_ => {
1297+
err.span_note(var_origin.span, "the type here was inferred to `!`");
1298+
}
1299+
}
1300+
1301+
err.span_note(diverging_var_span, "... due to this expression").emit();
12891302
}
12901303
}
12911304
}

0 commit comments

Comments
 (0)