Skip to content

Commit c0ed1dd

Browse files
committed
Relax overly strict mgca requirements
1 parent 07254c2 commit c0ed1dd

File tree

5 files changed

+34
-76
lines changed

5 files changed

+34
-76
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ impl Path {
123123
pub fn is_global(&self) -> bool {
124124
self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
125125
}
126+
127+
// FIXME: add docs
128+
#[tracing::instrument(level = "debug", ret)]
129+
pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool {
130+
allow_mgca_arg
131+
|| self.segments.len() == 1 && self.segments.iter().all(|seg| seg.args.is_none())
132+
}
126133
}
127134

128135
/// A segment of a path: an identifier, an optional lifetime, and a set of types.
@@ -1171,6 +1178,29 @@ pub struct Expr {
11711178
}
11721179

11731180
impl Expr {
1181+
// FIXME: update docs
1182+
/// Could this expr be either `N`, or `{ N }`, where `N` is a const parameter.
1183+
///
1184+
/// If this is not the case, name resolution does not resolve `N` when using
1185+
/// `min_const_generics` as more complex expressions are not supported.
1186+
///
1187+
/// Does not ensure that the path resolves to a const param, the caller should check this.
1188+
/// This also does not consider macros, so it's only correct after macro-expansion.
1189+
pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool {
1190+
let this = self.maybe_unwrap_block();
1191+
if allow_mgca_arg {
1192+
matches!(this.kind, ExprKind::Path(..))
1193+
} else {
1194+
if let ExprKind::Path(None, path) = &this.kind
1195+
&& path.is_potential_trivial_const_arg(allow_mgca_arg)
1196+
{
1197+
true
1198+
} else {
1199+
false
1200+
}
1201+
}
1202+
}
1203+
11741204
/// Returns an expression with (when possible) *one* outter brace removed
11751205
pub fn maybe_unwrap_block(&self) -> &Expr {
11761206
if let ExprKind::Block(block, None) = &self.kind

compiler/rustc_ast/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod util {
2626
pub mod case;
2727
pub mod classify;
2828
pub mod comments;
29-
mod const_args;
3029
pub mod literal;
3130
pub mod parser;
3231
pub mod unicode;

compiler/rustc_ast/src/util/const_args.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11171117
{
11181118
if !res.matches_ns(Namespace::TypeNS)
11191119
// FIXME: should this only allow single-segment paths?
1120-
&& path.is_potential_trivial_const_arg(&None, self.tcx.features().min_generic_const_args())
1120+
&& path.is_potential_trivial_const_arg(self.tcx.features().min_generic_const_args())
11211121
{
11221122
debug!(
11231123
"lower_generic_arg: Lowering type argument as const argument: {:?}",
@@ -2085,7 +2085,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20852085
let tcx = self.tcx;
20862086

20872087
let ct_kind = if path
2088-
.is_potential_trivial_const_arg(&None, tcx.features().min_generic_const_args())
2088+
.is_potential_trivial_const_arg(tcx.features().min_generic_const_args())
20892089
&& (tcx.features().min_generic_const_args()
20902090
|| matches!(res, Res::Def(DefKind::ConstParam, _)))
20912091
{
@@ -2160,7 +2160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21602160
let maybe_res =
21612161
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
21622162
if let ExprKind::Path(qself, path) = &expr.kind
2163-
&& path.is_potential_trivial_const_arg(qself, tcx.features().min_generic_const_args())
2163+
&& path.is_potential_trivial_const_arg(tcx.features().min_generic_const_args())
21642164
&& (tcx.features().min_generic_const_args()
21652165
|| matches!(maybe_res, Some(Res::Def(DefKind::ConstParam, _))))
21662166
{

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
12001200
if let TyKind::Path(None, ref path) = ty.kind
12011201
// We cannot disambiguate multi-segment paths right now as that requires type
12021202
// checking.
1203-
&& path.is_potential_trivial_const_arg(&None, false)
1203+
&& path.is_potential_trivial_const_arg(false)
12041204
{
12051205
let mut check_ns = |ns| {
12061206
self.maybe_resolve_ident_in_lexical_scope(path.segments[0].ident, ns)

0 commit comments

Comments
 (0)