Skip to content

Commit

Permalink
Auto merge of #87648 - JulianKnodt:const_eq_constrain, r=oli-obk
Browse files Browse the repository at this point in the history
allow eq constraints on associated constants

Updates #70256

(cc `@varkor,` `@Centril)`
  • Loading branch information
bors committed Jan 18, 2022
2 parents 7531d2f + b77bb5c commit 7bc7be8
Show file tree
Hide file tree
Showing 83 changed files with 776 additions and 382 deletions.
54 changes: 9 additions & 45 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ dependencies = [
"cargo-test-macro",
"cargo-test-support",
"cargo-util",
"clap 3.0.6",
"clap",
"crates-io",
"crossbeam-utils",
"curl",
Expand Down Expand Up @@ -615,28 +615,13 @@ dependencies = [
"ansi_term 0.12.1",
"atty",
"bitflags",
"strsim 0.8.0",
"textwrap 0.11.0",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
"yaml-rust 0.3.5",
]

[[package]]
name = "clap"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1957aa4a5fb388f0a0a73ce7556c5b42025b874e5cdc2c670775e346e97adec0"
dependencies = [
"atty",
"bitflags",
"indexmap",
"os_str_bytes",
"strsim 0.10.0",
"termcolor",
"textwrap 0.14.2",
]

[[package]]
name = "clippy"
version = "0.1.60"
Expand Down Expand Up @@ -669,7 +654,7 @@ version = "0.0.1"
dependencies = [
"bytecount",
"cargo_metadata 0.14.0",
"clap 2.34.0",
"clap",
"indoc",
"itertools 0.10.1",
"opener",
Expand Down Expand Up @@ -1751,7 +1736,7 @@ name = "installer"
version = "0.0.0"
dependencies = [
"anyhow",
"clap 2.34.0",
"clap",
"flate2",
"lazy_static",
"num_cpus",
Expand Down Expand Up @@ -2190,7 +2175,7 @@ dependencies = [
"ammonia",
"anyhow",
"chrono",
"clap 2.34.0",
"clap",
"elasticlunr-rs",
"env_logger 0.7.1",
"handlebars",
Expand Down Expand Up @@ -2521,15 +2506,6 @@ dependencies = [
"winapi",
]

[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]

[[package]]
name = "output_vt100"
version = "0.1.2"
Expand Down Expand Up @@ -2934,7 +2910,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0b4b5faaf07040474e8af74a9e19ff167d5d204df5db5c5c765edecfb900358"
dependencies = [
"bitflags",
"clap 2.34.0",
"clap",
"derive_more",
"env_logger 0.7.1",
"humantime 2.0.1",
Expand Down Expand Up @@ -3282,7 +3258,7 @@ dependencies = [
name = "rustbook"
version = "0.1.0"
dependencies = [
"clap 2.34.0",
"clap",
"env_logger 0.7.1",
"mdbook",
]
Expand Down Expand Up @@ -4898,19 +4874,13 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"

[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"

[[package]]
name = "structopt"
version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40b9788f4202aa75c240ecc9c15c65185e6a39ccdeb0fd5d008b98825464c87c"
dependencies = [
"clap 2.34.0",
"clap",
"lazy_static",
"structopt-derive",
]
Expand Down Expand Up @@ -5081,12 +5051,6 @@ dependencies = [
"unicode-width",
]

[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"

[[package]]
name = "thiserror"
version = "1.0.30"
Expand Down
33 changes: 26 additions & 7 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub enum AngleBracketedArg {
/// Argument for a generic parameter.
Arg(GenericArg),
/// Constraint for an associated item.
Constraint(AssocTyConstraint),
Constraint(AssocConstraint),
}

impl AngleBracketedArg {
Expand Down Expand Up @@ -1843,19 +1843,38 @@ impl UintTy {
/// A constraint on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or
/// `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`).
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct AssocTyConstraint {
pub struct AssocConstraint {
pub id: NodeId,
pub ident: Ident,
pub gen_args: Option<GenericArgs>,
pub kind: AssocTyConstraintKind,
pub kind: AssocConstraintKind,
pub span: Span,
}

/// The kinds of an `AssocTyConstraint`.
/// The kinds of an `AssocConstraint`.
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum AssocTyConstraintKind {
/// E.g., `A = Bar` in `Foo<A = Bar>`.
Equality { ty: P<Ty> },
pub enum Term {
Ty(P<Ty>),
Const(AnonConst),
}

impl From<P<Ty>> for Term {
fn from(v: P<Ty>) -> Self {
Term::Ty(v)
}
}

impl From<AnonConst> for Term {
fn from(v: AnonConst) -> Self {
Term::Const(v)
}
}

/// The kinds of an `AssocConstraint`.
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum AssocConstraintKind {
/// E.g., `A = Bar`, `A = 3` in `Foo<A = Bar>` where A is an associated type.
Equality { term: Term },
/// E.g. `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`.
Bound { bounds: GenericBounds },
}
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub trait MutVisitor: Sized {
noop_visit_lifetime(l, self);
}

fn visit_ty_constraint(&mut self, t: &mut AssocTyConstraint) {
noop_visit_ty_constraint(t, self);
fn visit_constraint(&mut self, t: &mut AssocConstraint) {
noop_visit_constraint(t, self);
}

fn visit_foreign_mod(&mut self, nm: &mut ForeignMod) {
Expand Down Expand Up @@ -430,8 +430,8 @@ pub fn noop_flat_map_arm<T: MutVisitor>(mut arm: Arm, vis: &mut T) -> SmallVec<[
smallvec![arm]
}

pub fn noop_visit_ty_constraint<T: MutVisitor>(
AssocTyConstraint { id, ident, gen_args, kind, span }: &mut AssocTyConstraint,
pub fn noop_visit_constraint<T: MutVisitor>(
AssocConstraint { id, ident, gen_args, kind, span }: &mut AssocConstraint,
vis: &mut T,
) {
vis.visit_id(id);
Expand All @@ -440,12 +440,11 @@ pub fn noop_visit_ty_constraint<T: MutVisitor>(
vis.visit_generic_args(gen_args);
}
match kind {
AssocTyConstraintKind::Equality { ref mut ty } => {
vis.visit_ty(ty);
}
AssocTyConstraintKind::Bound { ref mut bounds } => {
visit_bounds(bounds, vis);
}
AssocConstraintKind::Equality { ref mut term } => match term {
Term::Ty(ty) => vis.visit_ty(ty),
Term::Const(c) => vis.visit_anon_const(c),
},
AssocConstraintKind::Bound { ref mut bounds } => visit_bounds(bounds, vis),
}
vis.visit_span(span);
}
Expand Down Expand Up @@ -555,7 +554,7 @@ pub fn noop_visit_angle_bracketed_parameter_data<T: MutVisitor>(
let AngleBracketedArgs { args, span } = data;
visit_vec(args, |arg| match arg {
AngleBracketedArg::Arg(arg) => vis.visit_generic_arg(arg),
AngleBracketedArg::Constraint(constraint) => vis.visit_ty_constraint(constraint),
AngleBracketedArg::Constraint(constraint) => vis.visit_constraint(constraint),
});
vis.visit_span(span);
}
Expand Down
20 changes: 9 additions & 11 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ pub trait Visitor<'ast>: Sized {
fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
walk_generic_arg(self, generic_arg)
}
fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
walk_assoc_ty_constraint(self, constraint)
fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
walk_assoc_constraint(self, constraint)
}
fn visit_attribute(&mut self, attr: &'ast Attribute) {
walk_attribute(self, attr)
Expand Down Expand Up @@ -464,7 +464,7 @@ where
for arg in &data.args {
match arg {
AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
AngleBracketedArg::Constraint(c) => visitor.visit_assoc_ty_constraint(c),
AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
}
}
}
Expand All @@ -486,19 +486,17 @@ where
}
}

pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
visitor: &mut V,
constraint: &'a AssocTyConstraint,
) {
pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
visitor.visit_ident(constraint.ident);
if let Some(ref gen_args) = constraint.gen_args {
visitor.visit_generic_args(gen_args.span(), gen_args);
}
match constraint.kind {
AssocTyConstraintKind::Equality { ref ty } => {
visitor.visit_ty(ty);
}
AssocTyConstraintKind::Bound { ref bounds } => {
AssocConstraintKind::Equality { ref term } => match term {
Term::Ty(ty) => visitor.visit_ty(ty),
Term::Const(c) => visitor.visit_anon_const(c),
},
AssocConstraintKind::Bound { ref bounds } => {
walk_list!(visitor, visit_param_bound, bounds);
}
}
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// returns a `hir::TypeBinding` representing `Item`.
fn lower_assoc_ty_constraint(
&mut self,
constraint: &AssocTyConstraint,
constraint: &AssocConstraint,
mut itctx: ImplTraitContext<'_, 'hir>,
) -> hir::TypeBinding<'hir> {
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
Expand Down Expand Up @@ -997,10 +997,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
};

let kind = match constraint.kind {
AssocTyConstraintKind::Equality { ref ty } => {
hir::TypeBindingKind::Equality { ty: self.lower_ty(ty, itctx) }
AssocConstraintKind::Equality { ref term } => {
let term = match term {
Term::Ty(ref ty) => self.lower_ty(ty, itctx).into(),
Term::Const(ref c) => self.lower_anon_const(c).into(),
};
hir::TypeBindingKind::Equality { term }
}
AssocTyConstraintKind::Bound { ref bounds } => {
AssocConstraintKind::Bound { ref bounds } => {
let mut capturable_lifetimes;
let mut parent_def_id = self.current_hir_id_owner;
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
Expand Down Expand Up @@ -1078,7 +1082,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
itctx,
);

hir::TypeBindingKind::Equality { ty }
hir::TypeBindingKind::Equality { term: ty.into() }
})
} else {
// Desugar `AssocTy: Bounds` into a type binding where the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ty: &'hir hir::Ty<'hir>,
) -> hir::TypeBinding<'hir> {
let ident = Ident::with_dummy_span(hir::FN_OUTPUT_NAME);
let kind = hir::TypeBindingKind::Equality { ty };
let kind = hir::TypeBindingKind::Equality { term: ty.into() };
let args = arena_vec![self;];
let bindings = arena_vec![self;];
let gen_args = self.arena.alloc(hir::GenericArgs {
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ impl<'a> AstValidator<'a> {
self.outer_impl_trait = old;
}

fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) {
fn visit_assoc_constraint_from_generic_args(&mut self, constraint: &'a AssocConstraint) {
match constraint.kind {
AssocTyConstraintKind::Equality { .. } => {}
AssocTyConstraintKind::Bound { .. } => {
AssocConstraintKind::Equality { .. } => {}
AssocConstraintKind::Bound { .. } => {
if self.is_assoc_ty_bound_banned {
self.err_handler().span_err(
constraint.span,
Expand All @@ -150,7 +150,7 @@ impl<'a> AstValidator<'a> {
}
}
}
self.visit_assoc_ty_constraint(constraint);
self.visit_assoc_constraint(constraint);
}

// Mirrors `visit::walk_ty`, but tracks relevant state.
Expand Down Expand Up @@ -1277,7 +1277,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// are allowed to contain nested `impl Trait`.
AngleBracketedArg::Constraint(constraint) => {
self.with_impl_trait(None, |this| {
this.visit_assoc_ty_constraint_from_generic_args(constraint);
this.visit_assoc_constraint_from_generic_args(constraint);
});
}
}
Expand Down Expand Up @@ -1586,12 +1586,12 @@ fn deny_equality_constraints(
let len = assoc_path.segments.len() - 1;
let gen_args = args.as_ref().map(|p| (**p).clone());
// Build `<Bar = RhsTy>`.
let arg = AngleBracketedArg::Constraint(AssocTyConstraint {
let arg = AngleBracketedArg::Constraint(AssocConstraint {
id: rustc_ast::node_id::DUMMY_NODE_ID,
ident: *ident,
gen_args,
kind: AssocTyConstraintKind::Equality {
ty: predicate.rhs_ty.clone(),
kind: AssocConstraintKind::Equality {
term: predicate.rhs_ty.clone().into(),
},
span: ident.span,
});
Expand Down
Loading

0 comments on commit 7bc7be8

Please sign in to comment.