Skip to content

Commit 4dcc713

Browse files
committed
Adjust and document 'Pat::to_ty' accordingly.
1 parent 02ef849 commit 4dcc713

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/libsyntax/ast.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,21 +519,28 @@ impl fmt::Debug for Pat {
519519
}
520520

521521
impl Pat {
522+
/// Attempt reparsing the pattern as a type.
523+
/// This is intended for use by diagnostics.
522524
pub(super) fn to_ty(&self) -> Option<P<Ty>> {
523525
let node = match &self.node {
526+
// In a type expression `_` is an inference variable.
524527
PatKind::Wild => TyKind::Infer,
528+
// An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
525529
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) => {
526530
TyKind::Path(None, Path::from_ident(*ident))
527531
}
528532
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
529533
PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
534+
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
530535
PatKind::Ref(pat, mutbl) => pat
531536
.to_ty()
532537
.map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
533-
PatKind::Slice(pats, None, _) if pats.len() == 1 => {
534-
pats[0].to_ty().map(TyKind::Slice)?
535-
}
536-
PatKind::Tuple(pats, None) => {
538+
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
539+
// when `P` can be reparsed as a type `T`.
540+
PatKind::Slice(pats) if pats.len() == 1 => pats[0].to_ty().map(TyKind::Slice)?,
541+
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
542+
// assuming `T0` to `Tn` are all syntactically valid as types.
543+
PatKind::Tuple(pats) => {
537544
let mut tys = Vec::with_capacity(pats.len());
538545
// FIXME(#48994) - could just be collected into an Option<Vec>
539546
for pat in pats {

0 commit comments

Comments
 (0)