Skip to content

Commit 6a19867

Browse files
committed
simplify validate_generic_param_order
1 parent da7ada5 commit 6a19867

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+36-39
Original file line numberDiff line numberDiff line change
@@ -889,48 +889,45 @@ fn validate_generic_param_order(
889889
) {
890890
let mut max_param: Option<ParamKindOrd> = None;
891891
let mut out_of_order = FxHashMap::default();
892-
let mut param_idents = vec![];
892+
let mut param_idents = Vec::with_capacity(generics.len());
893893

894-
for param in generics {
895-
let ident = Some(param.ident.to_string());
896-
let (kind, bounds, span) = (&param.kind, Some(&*param.bounds), param.ident.span);
894+
for (idx, param) in generics.iter().enumerate() {
895+
let ident = param.ident;
896+
let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
897897
let (ord_kind, ident) = match &param.kind {
898-
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
899-
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
898+
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
899+
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
900900
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
901901
let ty = pprust::ty_to_string(ty);
902902
let unordered = sess.features_untracked().unordered_const_ty_params();
903-
(ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty)))
903+
(ParamKindOrd::Const { unordered }, format!("const {}: {}", ident, ty))
904904
}
905905
};
906-
if let Some(ident) = ident {
907-
param_idents.push((kind, ord_kind, bounds, param_idents.len(), ident));
908-
}
909-
let max_param = &mut max_param;
906+
param_idents.push((kind, ord_kind, bounds, idx, ident));
910907
match max_param {
911-
Some(max_param) if *max_param > ord_kind => {
912-
let entry = out_of_order.entry(ord_kind).or_insert((*max_param, vec![]));
908+
Some(max_param) if max_param > ord_kind => {
909+
let entry = out_of_order.entry(ord_kind).or_insert((max_param, vec![]));
913910
entry.1.push(span);
914911
}
915-
Some(_) | None => *max_param = Some(ord_kind),
912+
Some(_) | None => max_param = Some(ord_kind),
916913
};
917914
}
918915

919-
let mut ordered_params = "<".to_string();
920916
if !out_of_order.is_empty() {
917+
let mut ordered_params = "<".to_string();
921918
param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i));
922919
let mut first = true;
923920
for (kind, _, bounds, _, ident) in param_idents {
924921
if !first {
925922
ordered_params += ", ";
926923
}
927924
ordered_params += &ident;
928-
if let Some(bounds) = bounds {
929-
if !bounds.is_empty() {
930-
ordered_params += ": ";
931-
ordered_params += &pprust::bounds_to_string(&bounds);
932-
}
925+
926+
if !bounds.is_empty() {
927+
ordered_params += ": ";
928+
ordered_params += &pprust::bounds_to_string(&bounds);
933929
}
930+
934931
match kind {
935932
GenericParamKind::Type { default: Some(default) } => {
936933
ordered_params += " = ";
@@ -946,32 +943,32 @@ fn validate_generic_param_order(
946943
}
947944
first = false;
948945
}
949-
}
950-
ordered_params += ">";
951946

952-
for (param_ord, (max_param, spans)) in &out_of_order {
953-
let mut err =
954-
handler.struct_span_err(
947+
ordered_params += ">";
948+
949+
for (param_ord, (max_param, spans)) in &out_of_order {
950+
let mut err = handler.struct_span_err(
955951
spans.clone(),
956952
&format!(
957953
"{} parameters must be declared prior to {} parameters",
958954
param_ord, max_param,
959955
),
960956
);
961-
err.span_suggestion(
962-
span,
963-
&format!(
964-
"reorder the parameters: lifetimes, {}",
965-
if sess.features_untracked().unordered_const_ty_params() {
966-
"then consts and types"
967-
} else {
968-
"then types, then consts"
969-
}
970-
),
971-
ordered_params.clone(),
972-
Applicability::MachineApplicable,
973-
);
974-
err.emit();
957+
err.span_suggestion(
958+
span,
959+
&format!(
960+
"reorder the parameters: lifetimes, {}",
961+
if sess.features_untracked().unordered_const_ty_params() {
962+
"then consts and types"
963+
} else {
964+
"then types, then consts"
965+
}
966+
),
967+
ordered_params.clone(),
968+
Applicability::MachineApplicable,
969+
);
970+
err.emit();
971+
}
975972
}
976973
}
977974

0 commit comments

Comments
 (0)