1
Fork 0

Handle parenthesised infer args

This commit is contained in:
Boxy 2025-01-20 06:34:57 +00:00
parent 1983c437ce
commit c58fe21cb9
3 changed files with 32 additions and 6 deletions

View file

@ -288,6 +288,7 @@ impl ParenthesizedArgs {
} }
} }
use crate::AstDeref;
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId}; pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
/// Modifiers on a trait bound like `~const`, `?` and `!`. /// Modifiers on a trait bound like `~const`, `?` and `!`.
@ -2166,6 +2167,14 @@ impl Ty {
} }
final_ty final_ty
} }
pub fn is_maybe_parenthesised_infer(&self) -> bool {
match &self.kind {
TyKind::Infer => true,
TyKind::Paren(inner) => inner.ast_deref().is_maybe_parenthesised_infer(),
_ => false,
}
}
} }
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]

View file

@ -1084,17 +1084,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match arg { match arg {
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)), ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
ast::GenericArg::Type(ty) => { ast::GenericArg::Type(ty) => {
match &ty.kind { // We cannot just match on `TyKind::Infer` as `(_)` is represented as
TyKind::Infer => { // `TyKind::Paren(TyKind::Infer)` and should also be lowered to `GenericArg::Infer`
if ty.is_maybe_parenthesised_infer() {
return GenericArg::Infer(hir::InferArg { return GenericArg::Infer(hir::InferArg {
hir_id: self.lower_node_id(ty.id), hir_id: self.lower_node_id(ty.id),
span: self.lower_span(ty.span), span: self.lower_span(ty.span),
}); });
} }
match &ty.kind {
// We parse const arguments as path types as we cannot distinguish them during // We parse const arguments as path types as we cannot distinguish them during
// parsing. We try to resolve that ambiguity by attempting resolution in both the // parsing. We try to resolve that ambiguity by attempting resolution in both the
// type and value namespaces. If we resolved the path in the value namespace, we // type and value namespaces. If we resolved the path in the value namespace, we
// transform it into a generic const argument. // transform it into a generic const argument.
//
// FIXME: Should we be handling `(PATH_TO_CONST)`?
TyKind::Path(None, path) => { TyKind::Path(None, path) => {
if let Some(res) = self if let Some(res) = self
.resolver .resolver

View file

@ -0,0 +1,12 @@
//@ check-pass
//@ revisions: gate nogate
#![cfg_attr(gate, feature(generic_arg_infer))]
fn main() {
// AST Types preserve parens for pretty printing reasons. This means
// that this is parsed as a `TyKind::Paren(TyKind::Infer)`. Generic
// arg lowering therefore needs to take into account not just `TyKind::Infer`
// but `TyKind::Infer` wrapped in arbitrarily many `TyKind::Paren`.
let a: Vec<(_)> = vec![1_u8];
let a: Vec<(((((_)))))> = vec![1_u8];
}