Skip to content

Commit c58fe21

Browse files
committed
Handle parenthesised infer args
1 parent 1983c43 commit c58fe21

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

compiler/rustc_ast/src/ast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl ParenthesizedArgs {
288288
}
289289
}
290290

291+
use crate::AstDeref;
291292
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
292293

293294
/// Modifiers on a trait bound like `~const`, `?` and `!`.
@@ -2166,6 +2167,14 @@ impl Ty {
21662167
}
21672168
final_ty
21682169
}
2170+
2171+
pub fn is_maybe_parenthesised_infer(&self) -> bool {
2172+
match &self.kind {
2173+
TyKind::Infer => true,
2174+
TyKind::Paren(inner) => inner.ast_deref().is_maybe_parenthesised_infer(),
2175+
_ => false,
2176+
}
2177+
}
21692178
}
21702179

21712180
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_ast_lowering/src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1084,17 +1084,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10841084
match arg {
10851085
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
10861086
ast::GenericArg::Type(ty) => {
1087+
// We cannot just match on `TyKind::Infer` as `(_)` is represented as
1088+
// `TyKind::Paren(TyKind::Infer)` and should also be lowered to `GenericArg::Infer`
1089+
if ty.is_maybe_parenthesised_infer() {
1090+
return GenericArg::Infer(hir::InferArg {
1091+
hir_id: self.lower_node_id(ty.id),
1092+
span: self.lower_span(ty.span),
1093+
});
1094+
}
1095+
10871096
match &ty.kind {
1088-
TyKind::Infer => {
1089-
return GenericArg::Infer(hir::InferArg {
1090-
hir_id: self.lower_node_id(ty.id),
1091-
span: self.lower_span(ty.span),
1092-
});
1093-
}
10941097
// We parse const arguments as path types as we cannot distinguish them during
10951098
// parsing. We try to resolve that ambiguity by attempting resolution in both the
10961099
// type and value namespaces. If we resolved the path in the value namespace, we
10971100
// transform it into a generic const argument.
1101+
//
1102+
// FIXME: Should we be handling `(PATH_TO_CONST)`?
10981103
TyKind::Path(None, path) => {
10991104
if let Some(res) = self
11001105
.resolver
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ check-pass
2+
//@ revisions: gate nogate
3+
#![cfg_attr(gate, feature(generic_arg_infer))]
4+
5+
fn main() {
6+
// AST Types preserve parens for pretty printing reasons. This means
7+
// that this is parsed as a `TyKind::Paren(TyKind::Infer)`. Generic
8+
// arg lowering therefore needs to take into account not just `TyKind::Infer`
9+
// but `TyKind::Infer` wrapped in arbitrarily many `TyKind::Paren`.
10+
let a: Vec<(_)> = vec![1_u8];
11+
let a: Vec<(((((_)))))> = vec![1_u8];
12+
}

0 commit comments

Comments
 (0)