pub struct PathSeg(pub DefId, pub usize);
pub trait AstConv<'gcx, 'tcx> {
- fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx>;
+ fn tcx<'a>(&'a self) -> TyCtxt<'gcx, 'tcx>;
/// Returns the set of bounds in scope for the type parameter with
/// the given id.
/// Report error if there is an explicit type parameter when using `impl Trait`.
fn check_impl_trait(
- tcx: TyCtxt<'_, '_, '_>,
+ tcx: TyCtxt<'_, '_>,
span: Span,
seg: &hir::PathSegment,
generics: &ty::Generics,
/// Checks that the correct number of generic arguments have been provided.
/// Used specifically for function calls.
pub fn check_generic_arg_count_for_call(
- tcx: TyCtxt<'_, '_, '_>,
+ tcx: TyCtxt<'_, '_>,
span: Span,
def: &ty::Generics,
seg: &hir::PathSegment,
/// Checks that the correct number of generic arguments have been provided.
/// This is used both for datatypes and function calls.
fn check_generic_arg_count(
- tcx: TyCtxt<'_, '_, '_>,
+ tcx: TyCtxt<'_, '_>,
span: Span,
def: &ty::Generics,
args: &hir::GenericArgs,
/// instantiate a `Kind`.
/// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
/// creates a suitable inference variable.
- pub fn create_substs_for_generic_args<'a, 'b>(
- tcx: TyCtxt<'a, 'gcx, 'tcx>,
+ pub fn create_substs_for_generic_args<'b>(
+ tcx: TyCtxt<'gcx, 'tcx>,
def_id: DefId,
parent_substs: &[Kind<'tcx>],
has_self: bool,
has_err
}
- pub fn prohibit_assoc_ty_binding(tcx: TyCtxt<'_, '_, '_>, span: Span) {
+ pub fn prohibit_assoc_ty_binding(tcx: TyCtxt<'_, '_>, span: Span) {
let mut err = struct_span_err!(tcx.sess, span, E0229,
"associated type bindings are not allowed here");
err.span_label(span, "associated type not allowed here").emit();
result_ty
}
+ /// Returns the `DefId` of the constant parameter that the provided expression is a path to.
+ pub fn const_param_def_id(&self, expr: &hir::Expr) -> Option<DefId> {
+ match &expr.node {
+ ExprKind::Path(hir::QPath::Resolved(_, path)) => match path.res {
+ Res::Def(DefKind::ConstParam, did) => Some(did),
+ _ => None,
+ },
+ _ => None,
+ }
+ }
+
pub fn ast_const_to_const(
&self,
ast_const: &hir::AnonConst,
}
}
- if let ExprKind::Path(ref qpath) = expr.node {
- if let hir::QPath::Resolved(_, ref path) = qpath {
- if let Res::Def(DefKind::ConstParam, def_id) = path.res {
- let node_id = tcx.hir().as_local_node_id(def_id).unwrap();
- let item_id = tcx.hir().get_parent_node(node_id);
- let item_def_id = tcx.hir().local_def_id(item_id);
- let generics = tcx.generics_of(item_def_id);
- let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(node_id)];
- let name = tcx.hir().name(node_id).as_interned_str();
- const_.val = ConstValue::Param(ty::ParamConst::new(index, name));
- }
- }
- };
+ if let Some(def_id) = self.const_param_def_id(expr) {
+ // Find the name and index of the const parameter by indexing the generics of the
+ // parent item and construct a `ParamConst`.
+ let node_id = tcx.hir().as_local_node_id(def_id).unwrap();
+ let item_id = tcx.hir().get_parent_node(node_id);
+ let item_def_id = tcx.hir().local_def_id(item_id);
+ let generics = tcx.generics_of(item_def_id);
+ let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(node_id)];
+ let name = tcx.hir().name(node_id).as_interned_str();
+ const_.val = ConstValue::Param(ty::ParamConst::new(index, name));
+ }
tcx.mk_const(const_)
}
pub implicitly_sized: Option<Span>,
}
-impl<'a, 'gcx, 'tcx> Bounds<'tcx> {
+impl<'gcx, 'tcx> Bounds<'tcx> {
/// Converts a bounds list into a flat set of predicates (like
/// where-clauses). Because some of our bounds listings (e.g.,
/// regions) don't include the self-type, you must supply the
/// self-type here (the `param_ty` parameter).
- pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, param_ty: Ty<'tcx>)
- -> Vec<(ty::Predicate<'tcx>, Span)>
- {
+ pub fn predicates(
+ &self,
+ tcx: TyCtxt<'gcx, 'tcx>,
+ param_ty: Ty<'tcx>,
+ ) -> Vec<(ty::Predicate<'tcx>, Span)> {
// If it could be sized, and is, add the `Sized` predicate.
let sized_predicate = self.implicitly_sized.and_then(|span| {
tcx.lang_items().sized_trait().map(|sized| {