Skip to content

Commit 24157ef

Browse files
committed
Tidy the code that checks for type parameters in associated const paths.
1 parent b5ec999 commit 24157ef

File tree

1 file changed

+27
-24
lines changed
  • src/librustc_typeck/check

1 file changed

+27
-24
lines changed

src/librustc_typeck/check/mod.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -3713,21 +3713,34 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37133713
&'a [ast::PathSegment],
37143714
def::Def)>
37153715
{
3716+
3717+
// Associated constants can't depend on generic types.
3718+
fn have_disallowed_generic_consts<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
3719+
def: def::Def,
3720+
ty: Ty<'tcx>,
3721+
span: Span,
3722+
node_id: ast::NodeId) -> bool {
3723+
match def {
3724+
def::DefAssociatedConst(..) => {
3725+
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3726+
fcx.sess().span_err(span,
3727+
"Associated consts cannot depend \
3728+
on type parameters or Self.");
3729+
fcx.write_error(node_id);
3730+
return true;
3731+
}
3732+
}
3733+
_ => {}
3734+
}
3735+
false
3736+
}
3737+
37163738
// If fully resolved already, we don't have to do anything.
37173739
if path_res.depth == 0 {
3718-
// Associated constants can't depend on generic types.
37193740
if let Some(ty) = opt_self_ty {
3720-
match path_res.full_def() {
3721-
def::DefAssociatedConst(..) => {
3722-
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3723-
fcx.sess().span_err(span,
3724-
"Associated consts cannot depend \
3725-
on type parameters or Self.");
3726-
fcx.write_error(node_id);
3727-
return None;
3728-
}
3729-
}
3730-
_ => {}
3741+
if have_disallowed_generic_consts(fcx, path_res.full_def(), ty,
3742+
span, node_id) {
3743+
return None;
37313744
}
37323745
}
37333746
Some((opt_self_ty, &path.segments, path_res.base_def))
@@ -3745,18 +3758,8 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37453758
let item_name = item_segment.identifier.name;
37463759
match method::resolve_ufcs(fcx, span, item_name, ty, node_id) {
37473760
Ok((def, lp)) => {
3748-
// Associated constants can't depend on generic types.
3749-
match def {
3750-
def::DefAssociatedConst(..) => {
3751-
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3752-
fcx.sess().span_err(span,
3753-
"Associated consts cannot depend \
3754-
on type parameters or Self.");
3755-
fcx.write_error(node_id);
3756-
return None;
3757-
}
3758-
}
3759-
_ => {}
3761+
if have_disallowed_generic_consts(fcx, def, ty, span, node_id) {
3762+
return None;
37603763
}
37613764
// Write back the new resolution.
37623765
fcx.ccx.tcx.def_map.borrow_mut()

0 commit comments

Comments
 (0)