Skip to content

Commit a9aba71

Browse files
authored
Unrolled build for #152139
Rollup merge of #152139 - khyperia:mgca-negative-literals, r=BoxyUwU mGCA: Support directly represented negated literals fixes #152123 PatExprKind already awkwardly tacks on a `negated: bool` for the same purpose: https://github.com/rust-lang/rust/blob/8bccf1224deab49b54694c9090e577bfe90a94e6/compiler/rustc_hir/src/hir.rs#L1954-L1959 perhaps one day we should indeed do that FIXME... r? @BoxyUwU
2 parents bce89b6 + 54a9be4 commit a9aba71

File tree

11 files changed

+70
-19
lines changed

11 files changed

+70
-19
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2570,7 +2570,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25702570

25712571
ConstArg {
25722572
hir_id: self.lower_node_id(expr.id),
2573-
kind: hir::ConstArgKind::Literal(literal.node),
2573+
kind: hir::ConstArgKind::Literal { lit: literal.node, negated: false },
2574+
span,
2575+
}
2576+
}
2577+
ExprKind::Unary(UnOp::Neg, inner_expr)
2578+
if let ExprKind::Lit(literal) = &inner_expr.kind =>
2579+
{
2580+
let span = expr.span;
2581+
let literal = self.lower_lit(literal, span);
2582+
2583+
ConstArg {
2584+
hir_id: self.lower_node_id(expr.id),
2585+
kind: hir::ConstArgKind::Literal { lit: literal.node, negated: true },
25742586
span,
25752587
}
25762588
}

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,10 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
521521
/// This variant is not always used to represent inference consts, sometimes
522522
/// [`GenericArg::Infer`] is used instead.
523523
Infer(Unambig),
524-
Literal(LitKind),
524+
Literal {
525+
lit: LitKind,
526+
negated: bool,
527+
},
525528
}
526529

527530
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -1958,8 +1961,6 @@ pub struct PatExpr<'hir> {
19581961
pub enum PatExprKind<'hir> {
19591962
Lit {
19601963
lit: Lit,
1961-
// FIXME: move this into `Lit` and handle negated literal expressions
1962-
// once instead of matching on unop neg expressions everywhere.
19631964
negated: bool,
19641965
},
19651966
/// A path pattern for a unit struct/variant or a (maybe-associated) constant.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
11101110
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
11111111
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
11121112
ConstArgKind::Error(_) => V::Result::output(), // errors and spans are not important
1113-
ConstArgKind::Literal(..) => V::Result::output(), // FIXME(mcga)
1113+
ConstArgKind::Literal { .. } => V::Result::output(), // FIXME(mcga)
11141114
}
11151115
}
11161116

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,8 +2396,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23962396
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
23972397
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
23982398
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
2399-
hir::ConstArgKind::Literal(kind) => {
2400-
self.lower_const_arg_literal(&kind, ty, const_arg.span)
2399+
hir::ConstArgKind::Literal { lit, negated } => {
2400+
self.lower_const_arg_literal(&lit, negated, ty, const_arg.span)
24012401
}
24022402
}
24032403
}
@@ -2804,9 +2804,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28042804
}
28052805

28062806
#[instrument(skip(self), level = "debug")]
2807-
fn lower_const_arg_literal(&self, kind: &LitKind, ty: Ty<'tcx>, span: Span) -> Const<'tcx> {
2807+
fn lower_const_arg_literal(
2808+
&self,
2809+
kind: &LitKind,
2810+
neg: bool,
2811+
ty: Ty<'tcx>,
2812+
span: Span,
2813+
) -> Const<'tcx> {
28082814
let tcx = self.tcx();
2809-
let input = LitToConstInput { lit: *kind, ty, neg: false };
2815+
let input = LitToConstInput { lit: *kind, ty, neg };
28102816
tcx.at(span).lit_to_const(input)
28112817
}
28122818

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,12 @@ impl<'a> State<'a> {
11581158
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11591159
ConstArgKind::Error(_) => self.word("/*ERROR*/"),
11601160
ConstArgKind::Infer(..) => self.word("_"),
1161-
ConstArgKind::Literal(node) => {
1161+
ConstArgKind::Literal { lit, negated } => {
1162+
if *negated {
1163+
self.word("-");
1164+
}
11621165
let span = const_arg.span;
1163-
self.print_literal(&Spanned { span, node: *node })
1166+
self.print_literal(&Spanned { span, node: *lit })
11641167
}
11651168
}
11661169
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14391439
| hir::ConstArgKind::TupleCall(..)
14401440
| hir::ConstArgKind::Tup(..)
14411441
| hir::ConstArgKind::Path(..)
1442-
| hir::ConstArgKind::Literal(..)
1442+
| hir::ConstArgKind::Literal { .. }
14431443
| hir::ConstArgKind::Infer(..) => true,
14441444
hir::ConstArgKind::Anon(..) => false,
14451445
},

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg<'tcx>) -> ConstantKind
334334
}
335335
hir::ConstArgKind::Anon(anon) => ConstantKind::Anonymous { body: anon.body },
336336
hir::ConstArgKind::Infer(..) | hir::ConstArgKind::Error(..) => ConstantKind::Infer,
337-
hir::ConstArgKind::Literal(..) => {
337+
hir::ConstArgKind::Literal { .. } => {
338338
ConstantKind::Path { path: "/* LITERAL */".to_string().into() }
339339
}
340340
}
@@ -1829,7 +1829,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18291829
| hir::ConstArgKind::TupleCall(..)
18301830
| hir::ConstArgKind::Tup(..)
18311831
| hir::ConstArgKind::Array(..)
1832-
| hir::ConstArgKind::Literal(..) => {
1832+
| hir::ConstArgKind::Literal { .. } => {
18331833
let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, cx.tcx.types.usize);
18341834
print_const(cx, ct)
18351835
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
325325
ConstArgKind::Infer(..) => chain!(self, "let ConstArgKind::Infer(..) = {const_arg}.kind"),
326326
ConstArgKind::Error(..) => chain!(self, "let ConstArgKind::Error(..) = {const_arg}.kind"),
327327
ConstArgKind::Tup(..) => chain!(self, "let ConstArgKind::Tup(..) = {const_arg}.kind"),
328-
ConstArgKind::Literal(..) => chain!(self, "let ConstArgKind::Literal(..) = {const_arg}.kind"),
328+
ConstArgKind::Literal { .. } => chain!(self, "let ConstArgKind::Literal {{ .. }} = {const_arg}.kind"),
329329
}
330330
}
331331

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ pub fn const_item_rhs_to_expr<'tcx>(tcx: TyCtxt<'tcx>, ct_rhs: ConstItemRhs<'tcx
11421142
ConstArgKind::Anon(anon) => Some(tcx.hir_body(anon.body).value),
11431143
ConstArgKind::Struct(..)
11441144
| ConstArgKind::Tup(..)
1145-
| ConstArgKind::Literal(..)
1145+
| ConstArgKind::Literal { .. }
11461146
| ConstArgKind::TupleCall(..)
11471147
| ConstArgKind::Array(..)
11481148
| ConstArgKind::Path(_)

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,16 @@ impl HirEqInterExpr<'_, '_, '_> {
686686
.zip(*args_b)
687687
.all(|(arg_a, arg_b)| self.eq_const_arg(arg_a, arg_b))
688688
},
689-
(ConstArgKind::Literal(kind_l), ConstArgKind::Literal(kind_r)) => kind_l == kind_r,
689+
(
690+
ConstArgKind::Literal {
691+
lit: kind_l,
692+
negated: negated_l,
693+
},
694+
ConstArgKind::Literal {
695+
lit: kind_r,
696+
negated: negated_r,
697+
},
698+
) => kind_l == kind_r && negated_l == negated_r,
690699
(ConstArgKind::Array(l_arr), ConstArgKind::Array(r_arr)) => {
691700
l_arr.elems.len() == r_arr.elems.len()
692701
&& l_arr
@@ -703,7 +712,7 @@ impl HirEqInterExpr<'_, '_, '_> {
703712
| ConstArgKind::TupleCall(..)
704713
| ConstArgKind::Infer(..)
705714
| ConstArgKind::Struct(..)
706-
| ConstArgKind::Literal(..)
715+
| ConstArgKind::Literal { .. }
707716
| ConstArgKind::Array(..)
708717
| ConstArgKind::Error(..),
709718
_,
@@ -1599,7 +1608,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
15991608
}
16001609
},
16011610
ConstArgKind::Infer(..) | ConstArgKind::Error(..) => {},
1602-
ConstArgKind::Literal(lit) => lit.hash(&mut self.s),
1611+
ConstArgKind::Literal { lit, negated } => {
1612+
lit.hash(&mut self.s);
1613+
negated.hash(&mut self.s);
1614+
},
16031615
}
16041616
}
16051617

0 commit comments

Comments
 (0)