Skip to content

Commit

Permalink
Handle parenthesised infer args
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jan 20, 2025
1 parent d17bee0 commit c03a819
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ impl ParenthesizedArgs {
}
}

use crate::AstDeref;
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};

/// Modifiers on a trait bound like `~const`, `?` and `!`.
Expand Down Expand Up @@ -2166,6 +2167,14 @@ impl 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)]
Expand Down
17 changes: 11 additions & 6 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,17 +1084,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match arg {
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
ast::GenericArg::Type(ty) => {
// We cannot just match on `TyKind::Infer` as `(_)` is represented as
// `TyKind::Paren(TyKind::Infer)` and should also be lowered to `GenericArg::Infer`
if ty.is_maybe_parenthesised_infer() {
return GenericArg::Infer(hir::InferArg {
hir_id: self.lower_node_id(ty.id),
span: self.lower_span(ty.span),
});
}

match &ty.kind {
TyKind::Infer => {
return GenericArg::Infer(hir::InferArg {
hir_id: self.lower_node_id(ty.id),
span: self.lower_span(ty.span),
});
}
// 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
// type and value namespaces. If we resolved the path in the value namespace, we
// transform it into a generic const argument.
//
// FIXME: Should we be handling `(PATH_TO_CONST)`?
TyKind::Path(None, path) => {
if let Some(res) = self
.resolver
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/const-generics/generic_arg_infer/parend_infer.rs
Original file line number Diff line number Diff line change
@@ -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];
}

0 comments on commit c03a819

Please sign in to comment.