Skip to content

Commit 794e50b

Browse files
Add helper for "simple" uses of type parameters
"Simple" uses have no qualified self type or generic parameters. They're just a path (e.g. `T::Item`).
1 parent 0c392d8 commit 794e50b

File tree

1 file changed

+21
-0
lines changed
  • compiler/rustc_builtin_macros/src/deriving/generic

1 file changed

+21
-0
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ use rustc_data_structures::map_in_place::MapInPlace;
189189
use rustc_expand::base::{Annotatable, ExtCtxt};
190190
use rustc_span::symbol::{kw, sym, Ident, Symbol};
191191
use rustc_span::Span;
192+
use smallvec::SmallVec;
192193

193194
use ty::{Bounds, Path, Ptr, PtrTy, Self_, Ty};
194195

@@ -337,6 +338,26 @@ struct TypeParameter {
337338
ty: P<ast::Ty>,
338339
}
339340

341+
impl TypeParameter {
342+
/// Returns the `Ident`s that comprise the `Path` for this type parameter if it is unqualified
343+
/// and has no bound generic params or segments with generic arguments.
344+
fn to_simple_path(&self) -> Option<SmallVec<[Ident; 4]>> {
345+
let TypeParameter { bound_generic_params, ty } = self;
346+
if !bound_generic_params.is_empty() {
347+
return None;
348+
}
349+
350+
let ast::TyKind::Path(None, path) = &ty.kind else { return None };
351+
352+
if path.segments.iter().any(|seg| seg.args.is_some()) {
353+
return None;
354+
}
355+
356+
let path = path.segments.iter().map(|seg| seg.ident).collect();
357+
Some(path)
358+
}
359+
}
360+
340361
/// This method helps to extract all the type parameters referenced from a
341362
/// type. For a type parameter `<T>`, it looks for either a `TyPath` that
342363
/// is not global and starts with `T`, or a `TyQPath`.

0 commit comments

Comments
 (0)