Skip to content

Commit cb3b2d8

Browse files
authored
Rollup merge of #150699 - literals-as-direct-const-args, r=BoxyUwU
MGCA: Support literals as direct const arguments Fixes [#150168](#150618) - **initial changes for mcga literals** - **rustfmt** r? @BoxyUwU
2 parents e7560df + 9f3956f commit cb3b2d8

12 files changed

Lines changed: 47 additions & 17 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25362536

25372537
overly_complex_const(self)
25382538
}
2539+
ExprKind::Lit(literal) => {
2540+
let span = expr.span;
2541+
let literal = self.lower_lit(literal, span);
2542+
2543+
ConstArg {
2544+
hir_id: self.lower_node_id(expr.id),
2545+
kind: hir::ConstArgKind::Literal(literal.node),
2546+
span,
2547+
}
2548+
}
25392549
_ => overly_complex_const(self),
25402550
}
25412551
}

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
518518
/// This variant is not always used to represent inference consts, sometimes
519519
/// [`GenericArg::Infer`] is used instead.
520520
Infer(Unambig),
521+
Literal(LitKind),
521522
}
522523

523524
#[derive(Clone, Copy, Debug, HashStable_Generic)]

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
11041104
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
11051105
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
11061106
ConstArgKind::Error(_) => V::Result::output(), // errors and spans are not important
1107+
ConstArgKind::Literal(..) => V::Result::output(), // FIXME(mcga)
11071108
}
11081109
}
11091110

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod generics;
2222
use std::assert_matches::assert_matches;
2323
use std::slice;
2424

25+
use rustc_ast::LitKind;
2526
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
2627
use rustc_errors::codes::*;
2728
use rustc_errors::{
@@ -2391,6 +2392,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23912392
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
23922393
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
23932394
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
2395+
hir::ConstArgKind::Literal(kind) if let FeedConstTy::WithTy(anon_const_type) = feed => {
2396+
self.lower_const_arg_literal(&kind, anon_const_type, const_arg.span)
2397+
}
2398+
hir::ConstArgKind::Literal(..) => {
2399+
let e = self.dcx().span_err(const_arg.span, "literal of unknown type");
2400+
ty::Const::new_error(tcx, e)
2401+
}
23942402
}
23952403
}
23962404

@@ -2773,6 +2781,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27732781
}
27742782
}
27752783

2784+
#[instrument(skip(self), level = "debug")]
2785+
fn lower_const_arg_literal(&self, kind: &LitKind, ty: Ty<'tcx>, span: Span) -> Const<'tcx> {
2786+
let tcx = self.tcx();
2787+
let input = LitToConstInput { lit: *kind, ty, neg: false };
2788+
tcx.at(span).lit_to_const(input)
2789+
}
2790+
27762791
#[instrument(skip(self), level = "debug")]
27772792
fn try_lower_anon_const_lit(
27782793
&self,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::{
2121
GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind,
2222
PreciseCapturingArg, RangeEnd, Term, TyPatKind,
2323
};
24-
use rustc_span::source_map::SourceMap;
24+
use rustc_span::source_map::{SourceMap, Spanned};
2525
use rustc_span::{DUMMY_SP, FileName, Ident, Span, Symbol, kw, sym};
2626
use {rustc_ast as ast, rustc_hir as hir};
2727

@@ -1157,6 +1157,10 @@ impl<'a> State<'a> {
11571157
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11581158
ConstArgKind::Error(_) => self.word("/*ERROR*/"),
11591159
ConstArgKind::Infer(..) => self.word("_"),
1160+
ConstArgKind::Literal(node) => {
1161+
let span = const_arg.span;
1162+
self.print_literal(&Spanned { span, node: *node })
1163+
}
11601164
}
11611165
}
11621166

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14441444
| hir::ConstArgKind::TupleCall(..)
14451445
| hir::ConstArgKind::Tup(..)
14461446
| hir::ConstArgKind::Path(..)
1447+
| hir::ConstArgKind::Literal(..)
14471448
| hir::ConstArgKind::Infer(..) => true,
14481449
hir::ConstArgKind::Anon(..) => false,
14491450
},

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg<'tcx>) -> ConstantKind
332332
}
333333
hir::ConstArgKind::Anon(anon) => ConstantKind::Anonymous { body: anon.body },
334334
hir::ConstArgKind::Infer(..) | hir::ConstArgKind::Error(..) => ConstantKind::Infer,
335+
hir::ConstArgKind::Literal(..) => {
336+
ConstantKind::Path { path: "/* LITERAL */".to_string().into() }
337+
}
335338
}
336339
}
337340

@@ -1814,7 +1817,8 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18141817
hir::ConstArgKind::Struct(..)
18151818
| hir::ConstArgKind::Path(..)
18161819
| hir::ConstArgKind::TupleCall(..)
1817-
| hir::ConstArgKind::Tup(..) => {
1820+
| hir::ConstArgKind::Tup(..)
1821+
| hir::ConstArgKind::Literal(..) => {
18181822
let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, FeedConstTy::No);
18191823
print_const(cx, ct)
18201824
}

src/tools/clippy/clippy_lints/src/utils/author.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
324324
ConstArgKind::Tup(..) => chain!(self, "let ConstArgKind::Tup(..) = {const_arg}.kind"),
325325
ConstArgKind::Infer(..) => chain!(self, "let ConstArgKind::Infer(..) = {const_arg}.kind"),
326326
ConstArgKind::Error(..) => chain!(self, "let ConstArgKind::Error(..) = {const_arg}.kind"),
327+
ConstArgKind::Literal(..) => chain!(self, "let ConstArgKind::Literal(..) = {const_arg}.kind")
327328
}
328329
}
329330

src/tools/clippy/clippy_utils/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ pub fn const_item_rhs_to_expr<'tcx>(tcx: TyCtxt<'tcx>, ct_rhs: ConstItemRhs<'tcx
11401140
ConstItemRhs::Body(body_id) => Some(tcx.hir_body(body_id).value),
11411141
ConstItemRhs::TypeConst(const_arg) => match const_arg.kind {
11421142
ConstArgKind::Anon(anon) => Some(tcx.hir_body(anon.body).value),
1143-
ConstArgKind::Struct(..) | ConstArgKind::TupleCall(..) | ConstArgKind::Tup(..) | ConstArgKind::Path(_) | ConstArgKind::Error(..) | ConstArgKind::Infer(..) => {
1143+
ConstArgKind::Struct(..) | ConstArgKind::TupleCall(..) | ConstArgKind::Tup(..) | ConstArgKind::Path(_) | ConstArgKind::Error(..) | ConstArgKind::Infer(..) | ConstArgKind::Literal(..) => {
11441144
None
11451145
},
11461146
},

src/tools/clippy/clippy_utils/src/hir_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ impl HirEqInterExpr<'_, '_, '_> {
689689
.zip(*args_b)
690690
.all(|(arg_a, arg_b)| self.eq_const_arg(arg_a, arg_b))
691691
}
692+
(ConstArgKind::Literal(kind_l), ConstArgKind::Literal(kind_r)) => {
693+
kind_l == kind_r
694+
},
692695
// Use explicit match for now since ConstArg is undergoing flux.
693696
(
694697
ConstArgKind::Path(..)
@@ -697,6 +700,7 @@ impl HirEqInterExpr<'_, '_, '_> {
697700
| ConstArgKind::Tup(..)
698701
| ConstArgKind::Infer(..)
699702
| ConstArgKind::Struct(..)
703+
| ConstArgKind::Literal(..)
700704
| ConstArgKind::Error(..),
701705
_,
702706
) => false,
@@ -1577,6 +1581,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
15771581
}
15781582
},
15791583
ConstArgKind::Infer(..) | ConstArgKind::Error(..) => {},
1584+
ConstArgKind::Literal(lit) => lit.hash(&mut self.s)
15801585
}
15811586
}
15821587

0 commit comments

Comments
 (0)