Skip to content

Commit efc9e1b

Browse files
committed
Auto merge of #152239 - JonathanBrouwer:rollup-2BolGX5, r=JonathanBrouwer
Rollup of 3 pull requests Successful merges: - #152129 (MGCA: require #[type_const] on free consts too) - #152139 (mGCA: Support directly represented negated literals) - #152189 (Convert to inline diagnostics in `rustc_passes`)
2 parents bce89b6 + ce8d09b commit efc9e1b

File tree

32 files changed

+632
-912
lines changed

32 files changed

+632
-912
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,7 +3789,6 @@ dependencies = [
37893789
"rustc_mir_build",
37903790
"rustc_mir_transform",
37913791
"rustc_parse",
3792-
"rustc_passes",
37933792
"rustc_public",
37943793
"rustc_resolve",
37953794
"rustc_session",
@@ -4417,7 +4416,6 @@ dependencies = [
44174416
"rustc_errors",
44184417
"rustc_expand",
44194418
"rustc_feature",
4420-
"rustc_fluent_macro",
44214419
"rustc_hir",
44224420
"rustc_index",
44234421
"rustc_macros",

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_driver_impl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ rustc_middle = { path = "../rustc_middle" }
2929
rustc_mir_build = { path = "../rustc_mir_build" }
3030
rustc_mir_transform = { path = "../rustc_mir_transform" }
3131
rustc_parse = { path = "../rustc_parse" }
32-
rustc_passes = { path = "../rustc_passes" }
3332
rustc_public = { path = "../rustc_public", features = ["rustc_internal"] }
3433
rustc_resolve = { path = "../rustc_resolve" }
3534
rustc_session = { path = "../rustc_session" }

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub static DEFAULT_LOCALE_RESOURCES: &[&str] = &[
117117
rustc_lint::DEFAULT_LOCALE_RESOURCE,
118118
rustc_mir_build::DEFAULT_LOCALE_RESOURCE,
119119
rustc_parse::DEFAULT_LOCALE_RESOURCE,
120-
rustc_passes::DEFAULT_LOCALE_RESOURCE,
121120
// tidy-alphabetical-end
122121
];
123122

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: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,14 +1422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14221422
LowerTypeRelativePathMode::Const,
14231423
)? {
14241424
TypeRelativePath::AssocItem(def_id, args) => {
1425-
if !self.tcx().is_type_const(def_id) {
1426-
let mut err = self.dcx().struct_span_err(
1427-
span,
1428-
"use of trait associated const without `#[type_const]`",
1429-
);
1430-
err.note("the declaration in the trait must be marked with `#[type_const]`");
1431-
return Err(err.emit());
1432-
}
1425+
self.require_type_const_attribute(def_id, span)?;
14331426
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
14341427
let ct = self.check_param_uses_if_mcg(ct, span, false);
14351428
Ok(ct)
@@ -1885,30 +1878,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18851878
item_def_id: DefId,
18861879
trait_segment: Option<&hir::PathSegment<'tcx>>,
18871880
item_segment: &hir::PathSegment<'tcx>,
1888-
) -> Const<'tcx> {
1889-
match self.lower_resolved_assoc_item_path(
1881+
) -> Result<Const<'tcx>, ErrorGuaranteed> {
1882+
let (item_def_id, item_args) = self.lower_resolved_assoc_item_path(
18901883
span,
18911884
opt_self_ty,
18921885
item_def_id,
18931886
trait_segment,
18941887
item_segment,
18951888
ty::AssocTag::Const,
1896-
) {
1897-
Ok((item_def_id, item_args)) => {
1898-
if !self.tcx().is_type_const(item_def_id) {
1899-
let mut err = self.dcx().struct_span_err(
1900-
span,
1901-
"use of `const` in the type system without `#[type_const]`",
1902-
);
1903-
err.note("the declaration must be marked with `#[type_const]`");
1904-
return Const::new_error(self.tcx(), err.emit());
1905-
}
1906-
1907-
let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
1908-
Const::new_unevaluated(self.tcx(), uv)
1909-
}
1910-
Err(guar) => Const::new_error(self.tcx(), guar),
1911-
}
1889+
)?;
1890+
self.require_type_const_attribute(item_def_id, span)?;
1891+
let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
1892+
Ok(Const::new_unevaluated(self.tcx(), uv))
19121893
}
19131894

19141895
/// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
@@ -2396,8 +2377,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23962377
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
23972378
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
23982379
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)
2380+
hir::ConstArgKind::Literal { lit, negated } => {
2381+
self.lower_const_arg_literal(&lit, negated, ty, const_arg.span)
24012382
}
24022383
}
24032384
}
@@ -2668,6 +2649,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26682649
self.lower_const_param(def_id, hir_id)
26692650
}
26702651
Res::Def(DefKind::Const, did) => {
2652+
if let Err(guar) = self.require_type_const_attribute(did, span) {
2653+
return Const::new_error(self.tcx(), guar);
2654+
}
2655+
26712656
assert_eq!(opt_self_ty, None);
26722657
let [leading_segments @ .., segment] = path.segments else { bug!() };
26732658
let _ = self
@@ -2718,6 +2703,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27182703
trait_segment,
27192704
path.segments.last().unwrap(),
27202705
)
2706+
.unwrap_or_else(|guar| Const::new_error(tcx, guar))
27212707
}
27222708
Res::Def(DefKind::Static { .. }, _) => {
27232709
span_bug!(span, "use of bare `static` ConstArgKind::Path's not yet supported")
@@ -2804,9 +2790,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28042790
}
28052791

28062792
#[instrument(skip(self), level = "debug")]
2807-
fn lower_const_arg_literal(&self, kind: &LitKind, ty: Ty<'tcx>, span: Span) -> Const<'tcx> {
2793+
fn lower_const_arg_literal(
2794+
&self,
2795+
kind: &LitKind,
2796+
neg: bool,
2797+
ty: Ty<'tcx>,
2798+
span: Span,
2799+
) -> Const<'tcx> {
28082800
let tcx = self.tcx();
2809-
let input = LitToConstInput { lit: *kind, ty, neg: false };
2801+
let input = LitToConstInput { lit: *kind, ty, neg };
28102802
tcx.at(span).lit_to_const(input)
28112803
}
28122804

@@ -2843,6 +2835,33 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28432835
.map(|l| tcx.at(expr.span).lit_to_const(l))
28442836
}
28452837

2838+
fn require_type_const_attribute(
2839+
&self,
2840+
def_id: DefId,
2841+
span: Span,
2842+
) -> Result<(), ErrorGuaranteed> {
2843+
let tcx = self.tcx();
2844+
if tcx.is_type_const(def_id) {
2845+
Ok(())
2846+
} else {
2847+
let mut err = self
2848+
.dcx()
2849+
.struct_span_err(span, "use of `const` in the type system without `#[type_const]`");
2850+
if def_id.is_local() {
2851+
let name = tcx.def_path_str(def_id);
2852+
err.span_suggestion(
2853+
tcx.def_span(def_id).shrink_to_lo(),
2854+
format!("add `#[type_const]` attribute to `{name}`"),
2855+
format!("#[type_const]\n"),
2856+
Applicability::MaybeIncorrect,
2857+
);
2858+
} else {
2859+
err.note("only consts marked with `#[type_const]` may be used in types");
2860+
}
2861+
Err(err.emit())
2862+
}
2863+
}
2864+
28462865
fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
28472866
let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
28482867
match idx {

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
},

compiler/rustc_passes/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ rustc_data_structures = { path = "../rustc_data_structures" }
1313
rustc_errors = { path = "../rustc_errors" }
1414
rustc_expand = { path = "../rustc_expand" }
1515
rustc_feature = { path = "../rustc_feature" }
16-
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1716
rustc_hir = { path = "../rustc_hir" }
1817
rustc_index = { path = "../rustc_index" }
1918
rustc_macros = { path = "../rustc_macros" }

0 commit comments

Comments
 (0)