//! fixed, but for the moment it's easier to do these checks early.
use crate::constrained_generic_params as cgp;
-use rustc::hir;
-use rustc::hir::itemlikevisit::ItemLikeVisitor;
-use rustc::hir::def_id::DefId;
-use rustc::ty::{self, TyCtxt, TypeFoldable};
use rustc::ty::query::Providers;
-use rustc::util::nodemap::{FxHashMap, FxHashSet};
+use rustc::ty::{self, TyCtxt, TypeFoldable};
+use rustc_data_structures::fx::{FxHashMap, FxHashSet};
+use rustc_hir as hir;
+use rustc_hir::def_id::DefId;
+use rustc_hir::itemlikevisit::ItemLikeVisitor;
use std::collections::hash_map::Entry::{Occupied, Vacant};
-use syntax_pos::Span;
+use rustc_span::Span;
use rustc_error_codes::*;
}
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: DefId) {
- tcx.hir().visit_item_likes_in_module(
- module_def_id,
- &mut ImplWfCheck { tcx }
- );
+ tcx.hir().visit_item_likes_in_module(module_def_id, &mut ImplWfCheck { tcx });
}
pub fn provide(providers: &mut Providers<'_>) {
- *providers = Providers {
- check_mod_impl_wf,
- ..*providers
- };
+ *providers = Providers { check_mod_impl_wf, ..*providers };
}
struct ImplWfCheck<'tcx> {
}
impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> {
- fn visit_item(&mut self, item: &'tcx hir::Item) {
+ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
if let hir::ItemKind::Impl(.., ref impl_item_refs) = item.kind {
let impl_def_id = self.tcx.hir().local_def_id(item.hir_id);
- enforce_impl_params_are_constrained(self.tcx,
- impl_def_id,
- impl_item_refs);
+ enforce_impl_params_are_constrained(self.tcx, impl_def_id, impl_item_refs);
enforce_impl_items_are_distinct(self.tcx, impl_item_refs);
}
}
- fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem) { }
+ fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem<'tcx>) {}
- fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) { }
+ fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem<'tcx>) {}
}
fn enforce_impl_params_are_constrained(
tcx: TyCtxt<'_>,
impl_def_id: DefId,
- impl_item_refs: &[hir::ImplItemRef],
+ impl_item_refs: &[hir::ImplItemRef<'_>],
) {
// Every lifetime used in an associated type must be constrained.
let impl_self_ty = tcx.type_of(impl_def_id);
// (#36836)
tcx.sess.delay_span_bug(
tcx.def_span(impl_def_id),
- "potentially unconstrained type parameters weren't evaluated",
+ &format!(
+ "potentially unconstrained type parameters weren't evaluated: {:?}",
+ impl_self_ty,
+ ),
);
return;
}
let mut input_parameters = cgp::parameters_for_impl(impl_self_ty, impl_trait_ref);
cgp::identify_constrained_generic_params(
- tcx, impl_predicates, impl_trait_ref, &mut input_parameters);
+ tcx,
+ impl_predicates,
+ impl_trait_ref,
+ &mut input_parameters,
+ );
// Disallow unconstrained lifetimes, but only if they appear in assoc types.
- let lifetimes_in_associated_types: FxHashSet<_> = impl_item_refs.iter()
+ let lifetimes_in_associated_types: FxHashSet<_> = impl_item_refs
+ .iter()
.map(|item_ref| tcx.hir().local_def_id(item_ref.id.hir_id))
.filter(|&def_id| {
let item = tcx.associated_item(def_id);
item.kind == ty::AssocKind::Type && item.defaultness.has_value()
})
- .flat_map(|def_id| {
- cgp::parameters_for(&tcx.type_of(def_id), true)
- }).collect();
+ .flat_map(|def_id| cgp::parameters_for(&tcx.type_of(def_id), true))
+ .collect();
for param in &impl_generics.params {
match param.kind {
ty::GenericParamDefKind::Type { .. } => {
let param_ty = ty::ParamTy::for_def(param);
if !input_parameters.contains(&cgp::Parameter::from(param_ty)) {
- report_unused_parameter(tcx,
- tcx.def_span(param.def_id),
- "type",
- ¶m_ty.to_string());
+ report_unused_parameter(
+ tcx,
+ tcx.def_span(param.def_id),
+ "type",
+ ¶m_ty.to_string(),
+ );
}
}
ty::GenericParamDefKind::Lifetime => {
let param_lt = cgp::Parameter::from(param.to_early_bound_region_data());
if lifetimes_in_associated_types.contains(¶m_lt) && // (*)
- !input_parameters.contains(¶m_lt) {
- report_unused_parameter(tcx,
- tcx.def_span(param.def_id),
- "lifetime",
- ¶m.name.to_string());
+ !input_parameters.contains(¶m_lt)
+ {
+ report_unused_parameter(
+ tcx,
+ tcx.def_span(param.def_id),
+ "lifetime",
+ ¶m.name.to_string(),
+ );
}
}
ty::GenericParamDefKind::Const => {
let param_ct = ty::ParamConst::for_def(param);
if !input_parameters.contains(&cgp::Parameter::from(param_ct)) {
- report_unused_parameter(tcx,
- tcx.def_span(param.def_id),
- "const",
- ¶m_ct.to_string());
+ report_unused_parameter(
+ tcx,
+ tcx.def_span(param.def_id),
+ "const",
+ ¶m_ct.to_string(),
+ );
}
}
}
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: &str) {
struct_span_err!(
- tcx.sess, span, E0207,
+ tcx.sess,
+ span,
+ E0207,
"the {} parameter `{}` is not constrained by the \
impl trait, self type, or predicates",
- kind, name)
- .span_label(span, format!("unconstrained {} parameter", kind))
- .emit();
+ kind,
+ name
+ )
+ .span_label(span, format!("unconstrained {} parameter", kind))
+ .emit();
}
/// Enforce that we do not have two items in an impl with the same name.
-fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplItemRef]) {
+fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplItemRef<'_>]) {
let mut seen_type_items = FxHashMap::default();
let mut seen_value_items = FxHashMap::default();
for impl_item_ref in impl_item_refs {
let impl_item = tcx.hir().impl_item(impl_item_ref.id);
let seen_items = match impl_item.kind {
hir::ImplItemKind::TyAlias(_) => &mut seen_type_items,
- _ => &mut seen_value_items,
+ _ => &mut seen_value_items,
};
match seen_items.entry(impl_item.ident.modern()) {
Occupied(entry) => {
- let mut err = struct_span_err!(tcx.sess, impl_item.span, E0201,
- "duplicate definitions with name `{}`:",
- impl_item.ident);
- err.span_label(*entry.get(),
- format!("previous definition of `{}` here",
- impl_item.ident));
+ let mut err = struct_span_err!(
+ tcx.sess,
+ impl_item.span,
+ E0201,
+ "duplicate definitions with name `{}`:",
+ impl_item.ident
+ );
+ err.span_label(
+ *entry.get(),
+ format!("previous definition of `{}` here", impl_item.ident),
+ );
err.span_label(impl_item.span, "duplicate definition");
err.emit();
}