Skip to content

Commit ae49732

Browse files
authored
Rollup merge of #101573 - lcnr:param-kind-ord, r=BoxyUwU
update `ParamKindOrd` #90207 (comment) 😁 writing comments "for future prs" sure works well :3 r? `@BoxyUwU`
2 parents fcdd501 + 5db6907 commit ae49732

37 files changed

+116
-161
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 and 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
@@ -839,10 +839,10 @@ fn validate_generic_param_order(
839839
let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
840840
let (ord_kind, ident) = match &param.kind {
841841
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
842-
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
842+
GenericParamKind::Type { default: _ } => (ParamKindOrd::TypeOrConst, ident.to_string()),
843843
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
844844
let ty = pprust::ty_to_string(ty);
845-
(ParamKindOrd::Const, format!("const {}: {}", ident, ty))
845+
(ParamKindOrd::TypeOrConst, format!("const {}: {}", ident, ty))
846846
}
847847
};
848848
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);

src/test/ui/const-generics/argument_order.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: lifetime parameters must be declared prior to const parameters
1+
error: lifetime parameters must be declared prior to type and const parameters
22
--> $DIR/argument_order.rs:6:32
33
|
44
LL | struct AlsoBad<const N: usize, 'a, T, 'b, const M: usize, U> {
@@ -11,7 +11,7 @@ LL | let _: AlsoBad<7, 'static, u32, 'static, 17, u16>;
1111
| ^^^^^^^
1212
|
1313
= note: lifetime arguments must be provided before type arguments
14-
= help: reorder the arguments: lifetimes, then consts: `<'a, 'b, N, T, M, U>`
14+
= help: reorder the arguments: lifetimes, then type and consts: `<'a, 'b, N, T, M, U>`
1515

1616
error: aborting due to 2 previous errors
1717

src/test/ui/const-generics/const-param-before-other-params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn bar<const X: u8, 'a>(_: &'a ()) {
2-
//~^ ERROR lifetime parameters must be declared prior to const parameters
2+
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
33
}
44

55
fn foo<const X: u8, T>(_: &T) {}

src/test/ui/const-generics/const-param-before-other-params.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: lifetime parameters must be declared prior to const parameters
1+
error: lifetime parameters must be declared prior to type and const parameters
22
--> $DIR/const-param-before-other-params.rs:1:21
33
|
44
LL | fn bar<const X: u8, 'a>(_: &'a ()) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Checks that lifetimes cannot be interspersed between consts and types.
22

33
struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
4-
//~^ Error lifetime parameters must be declared prior to const parameters
4+
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
55

66
struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
7-
//~^ Error lifetime parameters must be declared prior to type parameters
7+
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
88

99
fn main() {}

src/test/ui/const-generics/defaults/intermixed-lifetime.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: lifetime parameters must be declared prior to const parameters
1+
error: lifetime parameters must be declared prior to type and const parameters
22
--> $DIR/intermixed-lifetime.rs:3:28
33
|
44
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
55
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
66

7-
error: lifetime parameters must be declared prior to type parameters
7+
error: lifetime parameters must be declared prior to type and const parameters
88
--> $DIR/intermixed-lifetime.rs:6:37
99
|
1010
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);

0 commit comments

Comments
 (0)