Skip to content

Commit

Permalink
Rollup merge of #101573 - lcnr:param-kind-ord, r=BoxyUwU
Browse files Browse the repository at this point in the history
update `ParamKindOrd`

#90207 (comment) 😁

writing comments "for future prs" sure works well :3

r? `@BoxyUwU`
  • Loading branch information
Dylan-DPC committed Sep 9, 2022
2 parents fcdd501 + 5db6907 commit ae49732
Show file tree
Hide file tree
Showing 37 changed files with 116 additions and 161 deletions.
36 changes: 3 additions & 33 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fmt;
use std::mem;
Expand Down Expand Up @@ -324,46 +323,17 @@ pub type GenericBounds = Vec<GenericBound>;
/// Specifies the enforced ordering for generic parameters. In the future,
/// if we wanted to relax this order, we could override `PartialEq` and
/// `PartialOrd`, to allow the kinds to be unordered.
#[derive(Hash, Clone, Copy)]
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ParamKindOrd {
Lifetime,
Type,
Const,
// `Infer` is not actually constructed directly from the AST, but is implicitly constructed
// during HIR lowering, and `ParamKindOrd` will implicitly order inferred variables last.
Infer,
}

impl Ord for ParamKindOrd {
fn cmp(&self, other: &Self) -> Ordering {
use ParamKindOrd::*;
let to_int = |v| match v {
Lifetime => 0,
Infer | Type | Const => 1,
};

to_int(*self).cmp(&to_int(*other))
}
}
impl PartialOrd for ParamKindOrd {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for ParamKindOrd {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
TypeOrConst,
}
impl Eq for ParamKindOrd {}

impl fmt::Display for ParamKindOrd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParamKindOrd::Lifetime => "lifetime".fmt(f),
ParamKindOrd::Type => "type".fmt(f),
ParamKindOrd::Const { .. } => "const".fmt(f),
ParamKindOrd::Infer => "infer".fmt(f),
ParamKindOrd::TypeOrConst => "type and const".fmt(f),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,10 @@ fn validate_generic_param_order(
let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
let (ord_kind, ident) = match &param.kind {
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
GenericParamKind::Type { default: _ } => (ParamKindOrd::TypeOrConst, ident.to_string()),
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
let ty = pprust::ty_to_string(ty);
(ParamKindOrd::Const, format!("const {}: {}", ident, ty))
(ParamKindOrd::TypeOrConst, format!("const {}: {}", ident, ty))
}
};
param_idents.push((kind, ord_kind, bounds, idx, ident));
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ impl GenericArg<'_> {
pub fn to_ord(&self) -> ast::ParamKindOrd {
match self {
GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
GenericArg::Type(_) => ast::ParamKindOrd::Type,
GenericArg::Const(_) => ast::ParamKindOrd::Const,
GenericArg::Infer(_) => ast::ParamKindOrd::Infer,
GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => {
ast::ParamKindOrd::TypeOrConst
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ impl GenericParamDefKind {
pub fn to_ord(&self) -> ast::ParamKindOrd {
match self {
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const,
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
ast::ParamKindOrd::TypeOrConst
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
let ty = item_ctxt.ast_ty_to_ty(hir_ty);

// Iterate through the generics of the projection to find the one that corresponds to
// the def_id that this query was called with. We filter to only const args here as a
// precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
// the def_id that this query was called with. We filter to only type and const args here
// as a precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
// but it can't hurt to be safe ^^
if let ty::Projection(projection) = ty.kind() {
let generics = tcx.generics_of(projection.item_def_id);
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/argument_order.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to const parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/argument_order.rs:6:32
|
LL | struct AlsoBad<const N: usize, 'a, T, 'b, const M: usize, U> {
Expand All @@ -11,7 +11,7 @@ LL | let _: AlsoBad<7, 'static, u32, 'static, 17, u16>;
| ^^^^^^^
|
= note: lifetime arguments must be provided before type arguments
= help: reorder the arguments: lifetimes, then consts: `<'a, 'b, N, T, M, U>`
= help: reorder the arguments: lifetimes, then type and consts: `<'a, 'b, N, T, M, U>`

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn bar<const X: u8, 'a>(_: &'a ()) {
//~^ ERROR lifetime parameters must be declared prior to const parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
}

fn foo<const X: u8, T>(_: &T) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to const parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/const-param-before-other-params.rs:1:21
|
LL | fn bar<const X: u8, 'a>(_: &'a ()) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/defaults/intermixed-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Checks that lifetimes cannot be interspersed between consts and types.

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

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

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: lifetime parameters must be declared prior to const parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/intermixed-lifetime.rs:3:28
|
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`

error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/intermixed-lifetime.rs:6:37
|
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct Foo<const M: usize = 10, 'a>(&'a u32);
//~^ Error lifetime parameters must be declared prior to const parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to const parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/param-order-err-pretty-prints-default.rs:1:33
|
LL | struct Foo<const M: usize = 10, 'a>(&'a u32);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generics/issue-59508-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct A;

impl A {
pub fn do_things<T, 'a, 'b: 'a>() {
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
println!("panic");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generics/issue-59508-1.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/issue-59508-1.rs:10:25
|
LL | pub fn do_things<T, 'a, 'b: 'a>() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generics/issue-59508.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct A;

impl A {
pub fn do_things<'a, 'b: 'a, T>() {
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
println!("panic");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generics/issue-59508.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct A;

impl A {
pub fn do_things<T, 'a, 'b: 'a>() {
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
println!("panic");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/generics/issue-59508.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/issue-59508.rs:10:25
|
LL | pub fn do_things<T, 'a, 'b: 'a>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![crate_type = "lib"]

struct S<T = (), 'a>(&'a T);
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/issue-80512-param-reordering-with-defaults.rs:3:18
|
LL | struct S<T = (), 'a>(&'a T);
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/generics/lifetime-before-type-params.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(unused)]
fn first<T, 'a, 'b>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
fn second<'a, T, 'b>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
fn third<T, U, 'a>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
fn fourth<'a, T, 'b, U, 'c, V>() {}
//~^ ERROR lifetime parameters must be declared prior to type parameters
//~^ ERROR lifetime parameters must be declared prior to type and const parameters

fn main() {}
8 changes: 4 additions & 4 deletions src/test/ui/generics/lifetime-before-type-params.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/lifetime-before-type-params.rs:2:13
|
LL | fn first<T, 'a, 'b>() {}
| ----^^--^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, 'b, T>`

error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/lifetime-before-type-params.rs:4:18
|
LL | fn second<'a, T, 'b>() {}
| --------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, 'b, T>`

error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/lifetime-before-type-params.rs:6:16
|
LL | fn third<T, U, 'a>() {}
| -------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, T, U>`

error: lifetime parameters must be declared prior to type parameters
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/lifetime-before-type-params.rs:8:18
|
LL | fn fourth<'a, T, 'b, U, 'c, V>() {}
Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/parser/issues/issue-14303-enum.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/issues/issue-14303-enum.stderr

This file was deleted.

4 changes: 0 additions & 4 deletions src/test/ui/parser/issues/issue-14303-fn-def.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/issues/issue-14303-fn-def.stderr

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/ui/parser/issues/issue-14303-impl.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/issues/issue-14303-impl.stderr

This file was deleted.

13 changes: 0 additions & 13 deletions src/test/ui/parser/issues/issue-14303-path.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/test/ui/parser/issues/issue-14303-path.stderr

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/ui/parser/issues/issue-14303-struct.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/issues/issue-14303-struct.stderr

This file was deleted.

4 changes: 0 additions & 4 deletions src/test/ui/parser/issues/issue-14303-trait.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/issues/issue-14303-trait.stderr

This file was deleted.

33 changes: 33 additions & 0 deletions src/test/ui/parser/issues/issue-14303.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
enum Enum<'a, T, 'b> {
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
A(&'a &'b T)
}

struct Struct<'a, T, 'b> {
//~^ ERROR lifetime parameters must be declared prior to type and const parameters
x: &'a &'b T
}

trait Trait<'a, T, 'b> {}
//~^ ERROR lifetime parameters must be declared prior to type and const parameters

fn foo<'a, T, 'b>(x: &'a T) {}
//~^ ERROR lifetime parameters must be declared prior to type and const parameters

struct Y<T>(T);
impl<'a, T, 'b> Y<T> {}
//~^ ERROR lifetime parameters must be declared prior to type and const parameters

mod bar {
pub struct X<'a, 'b, 'c, T> {
a: &'a str,
b: &'b str,
c: &'c str,
t: T,
}
}

fn bar<'a, 'b, 'c, T>(x: bar::X<'a, T, 'b, 'c>) {}
//~^ ERROR type provided when a lifetime was expected

fn main() {}
Loading

0 comments on commit ae49732

Please sign in to comment.