Skip to content

Commit b79a2b3

Browse files
committed
update ParamKindOrd
1 parent ccb5595 commit b79a2b3

File tree

5 files changed

+13
-42
lines changed

5 files changed

+13
-42
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3333
use rustc_span::source_map::{respan, Spanned};
3434
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3535
use rustc_span::{Span, DUMMY_SP};
36-
use std::cmp::Ordering;
3736
use std::convert::TryFrom;
3837
use std::fmt;
3938
use std::mem;
@@ -324,46 +323,17 @@ pub type GenericBounds = Vec<GenericBound>;
324323
/// Specifies the enforced ordering for generic parameters. In the future,
325324
/// if we wanted to relax this order, we could override `PartialEq` and
326325
/// `PartialOrd`, to allow the kinds to be unordered.
327-
#[derive(Hash, Clone, Copy)]
326+
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
328327
pub enum ParamKindOrd {
329328
Lifetime,
330-
Type,
331-
Const,
332-
// `Infer` is not actually constructed directly from the AST, but is implicitly constructed
333-
// during HIR lowering, and `ParamKindOrd` will implicitly order inferred variables last.
334-
Infer,
335-
}
336-
337-
impl Ord for ParamKindOrd {
338-
fn cmp(&self, other: &Self) -> Ordering {
339-
use ParamKindOrd::*;
340-
let to_int = |v| match v {
341-
Lifetime => 0,
342-
Infer | Type | Const => 1,
343-
};
344-
345-
to_int(*self).cmp(&to_int(*other))
346-
}
347-
}
348-
impl PartialOrd for ParamKindOrd {
349-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
350-
Some(self.cmp(other))
351-
}
352-
}
353-
impl PartialEq for ParamKindOrd {
354-
fn eq(&self, other: &Self) -> bool {
355-
self.cmp(other) == Ordering::Equal
356-
}
329+
TypeOrConst,
357330
}
358-
impl Eq for ParamKindOrd {}
359331

360332
impl fmt::Display for ParamKindOrd {
361333
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
362334
match self {
363335
ParamKindOrd::Lifetime => "lifetime".fmt(f),
364-
ParamKindOrd::Type => "type".fmt(f),
365-
ParamKindOrd::Const { .. } => "const".fmt(f),
366-
ParamKindOrd::Infer => "infer".fmt(f),
336+
ParamKindOrd::TypeOrConst => "type or const".fmt(f),
367337
}
368338
}
369339
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,10 @@ fn validate_generic_param_order(
845845
let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
846846
let (ord_kind, ident) = match &param.kind {
847847
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
848-
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
848+
GenericParamKind::Type { default: _ } => (ParamKindOrd::TypeOrConst, ident.to_string()),
849849
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
850850
let ty = pprust::ty_to_string(ty);
851-
(ParamKindOrd::Const, format!("const {}: {}", ident, ty))
851+
(ParamKindOrd::TypeOrConst, format!("const {}: {}", ident, ty))
852852
}
853853
};
854854
param_idents.push((kind, ord_kind, bounds, idx, ident));

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ impl GenericArg<'_> {
300300
pub fn to_ord(&self) -> ast::ParamKindOrd {
301301
match self {
302302
GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
303-
GenericArg::Type(_) => ast::ParamKindOrd::Type,
304-
GenericArg::Const(_) => ast::ParamKindOrd::Const,
305-
GenericArg::Infer(_) => ast::ParamKindOrd::Infer,
303+
GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => {
304+
ast::ParamKindOrd::TypeOrConst
305+
}
306306
}
307307
}
308308

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ impl GenericParamDefKind {
2727
pub fn to_ord(&self) -> ast::ParamKindOrd {
2828
match self {
2929
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
30-
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
31-
GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const,
30+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
31+
ast::ParamKindOrd::TypeOrConst
32+
}
3233
}
3334
}
3435

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
6565
let ty = item_ctxt.ast_ty_to_ty(hir_ty);
6666

6767
// Iterate through the generics of the projection to find the one that corresponds to
68-
// the def_id that this query was called with. We filter to only const args here as a
69-
// precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
68+
// the def_id that this query was called with. We filter to only type and const args here
69+
// as a precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
7070
// but it can't hurt to be safe ^^
7171
if let ty::Projection(projection) = ty.kind() {
7272
let generics = tcx.generics_of(projection.item_def_id);

0 commit comments

Comments
 (0)