Skip to content

Commit ee29d12

Browse files
committed
Relax overly strict mgca requirements
1 parent 71d3aed commit ee29d12

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.
@@ -1202,6 +1209,29 @@ pub struct Expr {
12021209
}
12031210

12041211
impl Expr {
1212+
// FIXME: update docs
1213+
/// Could this expr be either `N`, or `{ N }`, where `N` is a const parameter.
1214+
///
1215+
/// If this is not the case, name resolution does not resolve `N` when using
1216+
/// `min_const_generics` as more complex expressions are not supported.
1217+
///
1218+
/// Does not ensure that the path resolves to a const param, the caller should check this.
1219+
/// This also does not consider macros, so it's only correct after macro-expansion.
1220+
pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool {
1221+
let this = self.maybe_unwrap_block();
1222+
if allow_mgca_arg {
1223+
matches!(this.kind, ExprKind::Path(..))
1224+
} else {
1225+
if let ExprKind::Path(None, path) = &this.kind
1226+
&& path.is_potential_trivial_const_arg(allow_mgca_arg)
1227+
{
1228+
true
1229+
} else {
1230+
false
1231+
}
1232+
}
1233+
}
1234+
12051235
/// Returns an expression with (when possible) *one* outter brace removed
12061236
pub fn maybe_unwrap_block(&self) -> &Expr {
12071237
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
@@ -1095,7 +1095,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10951095
{
10961096
if !res.matches_ns(Namespace::TypeNS)
10971097
// FIXME: should this only allow single-segment paths?
1098-
&& path.is_potential_trivial_const_arg(&None, self.tcx.features().min_generic_const_args())
1098+
&& path.is_potential_trivial_const_arg(self.tcx.features().min_generic_const_args())
10991099
{
11001100
debug!(
11011101
"lower_generic_arg: Lowering type argument as const argument: {:?}",
@@ -2063,7 +2063,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20632063
let tcx = self.tcx;
20642064

20652065
let ct_kind = if path
2066-
.is_potential_trivial_const_arg(&None, tcx.features().min_generic_const_args())
2066+
.is_potential_trivial_const_arg(tcx.features().min_generic_const_args())
20672067
&& (tcx.features().min_generic_const_args()
20682068
|| matches!(res, Res::Def(DefKind::ConstParam, _)))
20692069
{
@@ -2138,7 +2138,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21382138
let maybe_res =
21392139
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
21402140
if let ExprKind::Path(qself, path) = &expr.kind
2141-
&& path.is_potential_trivial_const_arg(qself, tcx.features().min_generic_const_args())
2141+
&& path.is_potential_trivial_const_arg(tcx.features().min_generic_const_args())
21422142
&& (tcx.features().min_generic_const_args()
21432143
|| matches!(maybe_res, Some(Res::Def(DefKind::ConstParam, _))))
21442144
{

compiler/rustc_resolve/src/late.rs

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

0 commit comments

Comments
 (0)