Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Rework how type params are parsed #61254

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,11 @@ pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
}
}

pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
path_span: Span,
segment: &'v PathSegment) {
pub fn walk_path_segment<'v, V: Visitor<'v>>(
visitor: &mut V,
path_span: Span,
segment: &'v PathSegment,
) {
visitor.visit_ident(segment.ident);
if let Some(id) = segment.hir_id {
visitor.visit_id(id);
Expand All @@ -673,10 +675,12 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
}
}

pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
_path_span: Span,
generic_args: &'v GenericArgs) {
walk_list!(visitor, visit_generic_arg, &generic_args.args);
pub fn walk_generic_args<'v, V: Visitor<'v>>(
visitor: &mut V,
_path_span: Span,
generic_args: &'v GenericArgs,
) {
walk_list!(visitor, visit_generic_arg, &generic_args.args);
walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
}

Expand Down
46 changes: 19 additions & 27 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,22 +1334,6 @@ impl<'a> LoweringContext<'a> {
}
}

fn lower_generic_arg(&mut self,
arg: &ast::GenericArg,
itctx: ImplTraitContext<'_>)
-> hir::GenericArg {
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) => {
GenericArg::Const(ConstArg {
value: self.lower_anon_const(&ct),
span: ct.value.span,
})
}
}
}

fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_>) -> P<hir::Ty> {
P(self.lower_ty_direct(t, itctx))
}
Expand Down Expand Up @@ -2175,17 +2159,25 @@ impl<'a> LoweringContext<'a> {
param_mode: ParamMode,
mut itctx: ImplTraitContext<'_>,
) -> (hir::GenericArgs, bool) {
let &AngleBracketedArgs { ref args, ref bindings, .. } = data;
let has_types = args.iter().any(|arg| match arg {
ast::GenericArg::Type(_) => true,
_ => false,
});
(hir::GenericArgs {
args: args.iter().map(|a| self.lower_generic_arg(a, itctx.reborrow())).collect(),
bindings: bindings.iter().map(|b| self.lower_ty_binding(b, itctx.reborrow())).collect(),
parenthesized: false,
},
!has_types && param_mode == ParamMode::Optional)
let AngleBracketedArgs { lifetimes, types, const_args, bindings, .. } = &data;
let mut args = vec![];
for lt in lifetimes {
args.push(GenericArg::Lifetime(self.lower_lifetime(&lt)));
}
for ty in types {
args.push(GenericArg::Type(self.lower_ty_direct(&ty, itctx.reborrow())));
}
for ct in const_args {
args.push(GenericArg::Const(ConstArg {
value: self.lower_anon_const(&ct),
span: ct.value.span,
}))
}
let args = hir::HirVec::from(args);
let bindings = bindings.iter().map(|b| self.lower_ty_binding(b, itctx.reborrow()))
.collect();
(hir::GenericArgs { args, bindings, parenthesized: false },
types.is_empty() && param_mode == ParamMode::Optional)
}

fn lower_parenthesized_parameter_data(
Expand Down
19 changes: 14 additions & 5 deletions src/librustc/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,30 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> {
// Look up the type in the substitutions. It really should be in there.
let opt_ty = self.substs.get(p.index as usize).map(|k| k.unpack());
debug!(
"parameter `{:?}` ({:?}/{}), found {:?} when substituting (root type={:?}) substs={:?}",
p,
source_ty,
p.index,
opt_ty,
self.root_ty,
self.substs,
);
let ty = match opt_ty {
Some(UnpackedKind::Type(ty)) => ty,
Some(kind) => {
let span = self.span.unwrap_or(DUMMY_SP);
span_bug!(
span,
"expected type for `{:?}` ({:?}/{}) but found {:?} \
when substituting (root type={:?}) substs={:?}",
self.tcx.sess.delay_span_bug(span, &format!(
"expected type for `{:?}` ({:?}/{}) but found {:?} when substituting \
(root type={:?}) substs={:?}",
p,
source_ty,
p.index,
kind,
self.root_ty,
self.substs,
);
));
self.tcx.types.err
}
None => {
let span = self.span.unwrap_or(DUMMY_SP);
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,7 @@ impl<'a> ReplaceBodyWithLoop<'a> {
match seg.args.as_ref().map(|generic_arg| &**generic_arg) {
None => false,
Some(&ast::GenericArgs::AngleBracketed(ref data)) => {
let types = data.args.iter().filter_map(|arg| match arg {
ast::GenericArg::Type(ty) => Some(ty),
_ => None,
});
any_involves_impl_trait(types.into_iter()) ||
any_involves_impl_trait(data.types.iter()) ||
any_involves_impl_trait(data.bindings.iter().map(|b| &b.ty))
},
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
Expand Down
146 changes: 46 additions & 100 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use syntax::print::pprust;
use rustc::lint;
use rustc::lint::builtin::{BuiltinLintDiagnostics, NESTED_IMPL_TRAIT};
use rustc::session::Session;
use rustc_data_structures::fx::FxHashMap;
use syntax::ast::*;
use syntax::attr;
use syntax::source_map::Spanned;
Expand All @@ -21,7 +20,7 @@ use syntax::visit::{self, Visitor};
use syntax::{span_err, struct_span_err, walk_list};
use syntax_ext::proc_macro_decls::is_proc_macro_attr;
use syntax_pos::{Span, MultiSpan};
use errors::{Applicability, FatalError};
use errors::Applicability;
use log::debug;

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -346,104 +345,65 @@ impl<'a> AstValidator<'a> {
}
}

enum GenericPosition {
Param,
Arg,
}

fn validate_generics_order<'a>(
sess: &Session,
handler: &errors::Handler,
generics: impl Iterator<
Item = (
ParamKindOrd,
Option<&'a [GenericBound]>,
Span,
Option<String>
),
Item = (ParamKindOrd, Option<&'a [GenericBound]>, Span, Option<String>),
>,
pos: GenericPosition,
span: Span,
) {
let mut max_param: Option<ParamKindOrd> = None;
let mut out_of_order = FxHashMap::default();
let mut param_idents = vec![];
let mut found_type = false;
let mut found_const = false;

for (kind, bounds, span, ident) in generics {
if let Some(ident) = ident {
param_idents.push((kind, bounds, param_idents.len(), ident));
}
let max_param = &mut max_param;
match max_param {
Some(max_param) if *max_param > kind => {
let entry = out_of_order.entry(kind).or_insert((*max_param, vec![]));
entry.1.push(span);
}
Some(_) | None => *max_param = Some(kind),
let mut lifetimes = vec![];
let mut type_args = vec![];
let mut const_args = vec![];
let mut out_of_order = false;

for (kind, bounds, _span, ident) in generics {
let ident = match ident {
Some(ident) => ident,
None => return,
};
match kind {
ParamKindOrd::Type => found_type = true,
ParamKindOrd::Const => found_const = true,
_ => {}
ParamKindOrd::Lifetime => {
lifetimes.push((ident, bounds));
out_of_order |= type_args.len() > 0 || const_args.len() > 0;
}
ParamKindOrd::Type => {
type_args.push((ident, bounds));
out_of_order |= const_args.len() > 0;
}
ParamKindOrd::Const => {
const_args.push((ident, bounds));
}
}
}
if !out_of_order {
return;
}

let mut ordered_params = "<".to_string();
if !out_of_order.is_empty() {
param_idents.sort_by_key(|&(po, _, i, _)| (po, i));
let mut first = true;
for (_, bounds, _, ident) in param_idents {
if !first {
ordered_params += ", ";
}
ordered_params += &ident;
if let Some(bounds) = bounds {
if !bounds.is_empty() {
ordered_params += ": ";
ordered_params += &pprust::bounds_to_string(&bounds);
}
let mut first = true;
for (ident, bounds) in lifetimes.iter().chain(type_args.iter()).chain(const_args.iter()) {
if !first {
ordered_params += ", ";
}
ordered_params += &ident;
if let Some(bounds) = bounds {
if !bounds.is_empty() {
ordered_params += ": ";
ordered_params += &pprust::bounds_to_string(&bounds);
}
first = false;
}
first = false;
}
ordered_params += ">";

let pos_str = match pos {
GenericPosition::Param => "parameter",
GenericPosition::Arg => "argument",
};

for (param_ord, (max_param, spans)) in &out_of_order {
let mut err = handler.struct_span_err(spans.clone(),
&format!(
"{} {pos}s must be declared prior to {} {pos}s",
param_ord,
max_param,
pos = pos_str,
));
if let GenericPosition::Param = pos {
err.span_suggestion(
span,
&format!(
"reorder the {}s: lifetimes, then types{}",
pos_str,
if sess.features_untracked().const_generics { ", then consts" } else { "" },
),
ordered_params.clone(),
Applicability::MachineApplicable,
);
}
err.emit();
}

// FIXME(const_generics): we shouldn't have to abort here at all, but we currently get ICEs
// if we don't. Const parameters and type parameters can currently conflict if they
// are out-of-order.
if !out_of_order.is_empty() && found_type && found_const {
FatalError.raise();
}
handler.struct_span_err(span, "incorrect parameter order")
.span_suggestion(
span,
"reorder the parameters",
ordered_params.clone(),
Applicability::MachineApplicable,
).emit();
}

impl<'a> Visitor<'a> for AstValidator<'a> {
Expand Down Expand Up @@ -703,21 +663,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
match *generic_args {
GenericArgs::AngleBracketed(ref data) => {
walk_list!(self, visit_generic_arg, &data.args);
validate_generics_order(
self.session,
self.err_handler(),
data.args.iter().map(|arg| {
(match arg {
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
GenericArg::Type(..) => ParamKindOrd::Type,
GenericArg::Const(..) => ParamKindOrd::Const,
}, None, arg.span(), None)
}),
GenericPosition::Arg,
generic_args.span(),
);

walk_list!(self, visit_lifetime, &data.lifetimes);
walk_list!(self, visit_ty, &data.types);
walk_list!(self, visit_anon_const, &data.const_args);
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
// are allowed to contain nested `impl Trait`.
self.with_impl_trait(None, |this| {
Expand Down Expand Up @@ -750,7 +698,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

validate_generics_order(
self.session,
self.err_handler(),
generics.params.iter().map(|param| {
let ident = Some(param.ident.to_string());
Expand All @@ -764,7 +711,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
};
(kind, Some(&*param.bounds), param.ident.span, ident)
}),
GenericPosition::Param,
generics.span,
);

Expand Down
14 changes: 4 additions & 10 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
if let Some(ref generic_args) = seg.args {
match **generic_args {
ast::GenericArgs::AngleBracketed(ref data) => {
for arg in &data.args {
match arg {
ast::GenericArg::Type(ty) => self.visit_ty(ty),
_ => {}
}
for ty in &data.types {
self.visit_ty(ty);
}
}
ast::GenericArgs::Parenthesized(ref data) => {
Expand Down Expand Up @@ -861,11 +858,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
// Explicit types in the turbo-fish.
if let Some(ref generic_args) = seg.args {
if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
for arg in &data.args {
match arg {
ast::GenericArg::Type(ty) => self.visit_ty(ty),
_ => {}
}
for ty in &data.types {
self.visit_ty(ty);
}
}
}
Expand Down
Loading