Add lowering errors for const generics

Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
This commit is contained in:
varkor 2019-02-05 16:52:02 +01:00
parent 29f7206366
commit 2fec52bf58
3 changed files with 53 additions and 32 deletions

View File

@ -1157,6 +1157,15 @@ impl<'a> LoweringContext<'a> {
match arg {
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
ast::GenericArg::Type(ty) => GenericArg::Type(self.lower_ty_direct(&ty, itctx)),
ast::GenericArg::Const(ct) => {
// FIXME(const_generics): const generics are not yet defined in the HIR.
self.sess.struct_span_err(
ct.value.span,
"const generics in any position are currently unsupported",
).emit();
self.sess.abort_if_errors();
bug!();
}
}
}
@ -2441,7 +2450,7 @@ impl<'a> LoweringContext<'a> {
|this| this.lower_param_bounds(&param.bounds, itctx.reborrow()),
);
match param.kind {
let (name, kind) = match param.kind {
GenericParamKind::Lifetime => {
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
self.is_collecting_in_band_lifetimes = false;
@ -2457,22 +2466,14 @@ impl<'a> LoweringContext<'a> {
| hir::LifetimeName::Static => hir::ParamName::Plain(lt.name.ident()),
hir::LifetimeName::Error => ParamName::Error,
};
let param = hir::GenericParam {
id: lt.id,
hir_id: lt.hir_id,
name: param_name,
span: lt.span,
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
bounds,
kind: hir::GenericParamKind::Lifetime {
kind: hir::LifetimeParamKind::Explicit,
}
let kind = hir::GenericParamKind::Lifetime {
kind: hir::LifetimeParamKind::Explicit
};
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
param
(param_name, kind)
}
GenericParamKind::Type { ref default, .. } => {
// Don't expose `Self` (recovered "keyword used as ident" parse error).
@ -2491,27 +2492,41 @@ impl<'a> LoweringContext<'a> {
.chain(params)
.collect();
}
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(param.id);
hir::GenericParam {
id: node_id,
hir_id,
name: hir::ParamName::Plain(ident),
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
bounds,
span: ident.span,
kind: hir::GenericParamKind::Type {
default: default.as_ref().map(|x| {
self.lower_ty(x, ImplTraitContext::disallowed())
}),
synthetic: param.attrs.iter()
.filter(|attr| attr.check_name("rustc_synthetic"))
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
.next(),
}
}
let kind = hir::GenericParamKind::Type {
default: default.as_ref().map(|x| {
self.lower_ty(x, ImplTraitContext::disallowed())
}),
synthetic: param.attrs.iter()
.filter(|attr| attr.check_name("rustc_synthetic"))
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
.next(),
};
(hir::ParamName::Plain(ident), kind)
}
GenericParamKind::Const { .. } => {
// FIXME(const_generics): const generics are not yet defined in the HIR.
self.sess.struct_span_err(
param.ident.span,
"const generics in any position are currently unsupported",
).emit();
self.sess.abort_if_errors();
bug!();
}
};
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(param.id);
hir::GenericParam {
id: node_id,
hir_id,
name,
span: param.ident.span,
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
attrs: self.lower_attrs(&param.attrs),
bounds,
kind,
}
}

View File

@ -480,6 +480,7 @@ pub fn noop_visit_generic_arg<T: MutVisitor>(arg: &mut GenericArg, vis: &mut T)
match arg {
GenericArg::Lifetime(lt) => vis.visit_lifetime(lt),
GenericArg::Type(ty) => vis.visit_ty(ty),
GenericArg::Const(ct) => vis.visit_anon_const(ct),
}
}
@ -698,6 +699,9 @@ pub fn noop_visit_generic_param<T: MutVisitor>(param: &mut GenericParam, vis: &m
GenericParamKind::Type { default } => {
visit_opt(default, |default| vis.visit_ty(default));
}
GenericParamKind::Const { ty } => {
vis.visit_ty(ty);
}
}
}

View File

@ -126,6 +126,7 @@ pub trait Visitor<'ast>: Sized {
match generic_arg {
GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
GenericArg::Type(ty) => self.visit_ty(ty),
GenericArg::Const(ct) => self.visit_anon_const(ct),
}
}
fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
@ -486,6 +487,7 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Generi
match param.kind {
GenericParamKind::Lifetime => {}
GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
}
}