Skip to content

Commit 021fc25

Browse files
committed
Auto merge of #151594 - matthiaskrgr:rollup-sAb1Qar, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #149174 (`const` blocks as a `mod` item) - #151282 (THIR patterns: Explicitly distinguish `&pin` from plain `&`/`&mut`) - #151593 (miri subtree update) - #151516 (Do not emit errors on non-metaitem diagnostic attr input) r? @ghost
2 parents a18e6d9 + ac8e7f2 commit 021fc25

88 files changed

Lines changed: 1124 additions & 368 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_ast/src/ast.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3626,6 +3626,7 @@ impl Item {
36263626
pub fn opt_generics(&self) -> Option<&Generics> {
36273627
match &self.kind {
36283628
ItemKind::ExternCrate(..)
3629+
| ItemKind::ConstBlock(_)
36293630
| ItemKind::Use(_)
36303631
| ItemKind::Mod(..)
36313632
| ItemKind::ForeignMod(_)
@@ -3895,6 +3896,17 @@ impl ConstItemRhs {
38953896
}
38963897
}
38973898

3899+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3900+
pub struct ConstBlockItem {
3901+
pub id: NodeId,
3902+
pub span: Span,
3903+
pub block: Box<Block>,
3904+
}
3905+
3906+
impl ConstBlockItem {
3907+
pub const IDENT: Ident = Ident { name: kw::Underscore, span: DUMMY_SP };
3908+
}
3909+
38983910
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
38993911
#[derive(Clone, Encodable, Decodable, Debug)]
39003912
pub enum ItemKind {
@@ -3914,6 +3926,11 @@ pub enum ItemKind {
39143926
///
39153927
/// E.g., `const FOO: i32 = 42;`.
39163928
Const(Box<ConstItem>),
3929+
/// A module-level const block.
3930+
/// Equivalent to `const _: () = const { ... };`.
3931+
///
3932+
/// E.g., `const { assert!(true) }`.
3933+
ConstBlock(ConstBlockItem),
39173934
/// A function declaration (`fn`).
39183935
///
39193936
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -3990,6 +4007,8 @@ impl ItemKind {
39904007
| ItemKind::MacroDef(ident, _)
39914008
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
39924009

4010+
ItemKind::ConstBlock(_) => Some(ConstBlockItem::IDENT),
4011+
39934012
ItemKind::Use(_)
39944013
| ItemKind::ForeignMod(_)
39954014
| ItemKind::GlobalAsm(_)
@@ -4003,9 +4022,9 @@ impl ItemKind {
40034022
pub fn article(&self) -> &'static str {
40044023
use ItemKind::*;
40054024
match self {
4006-
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
4007-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
4008-
| Delegation(..) | DelegationMac(..) => "a",
4025+
Use(..) | Static(..) | Const(..) | ConstBlock(..) | Fn(..) | Mod(..)
4026+
| GlobalAsm(..) | TyAlias(..) | Struct(..) | Union(..) | Trait(..) | TraitAlias(..)
4027+
| MacroDef(..) | Delegation(..) | DelegationMac(..) => "a",
40094028
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
40104029
}
40114030
}
@@ -4016,6 +4035,7 @@ impl ItemKind {
40164035
ItemKind::Use(..) => "`use` import",
40174036
ItemKind::Static(..) => "static item",
40184037
ItemKind::Const(..) => "constant item",
4038+
ItemKind::ConstBlock(..) => "const block",
40194039
ItemKind::Fn(..) => "function",
40204040
ItemKind::Mod(..) => "module",
40214041
ItemKind::ForeignMod(..) => "extern block",
@@ -4045,7 +4065,18 @@ impl ItemKind {
40454065
| Self::Trait(box Trait { generics, .. })
40464066
| Self::TraitAlias(box TraitAlias { generics, .. })
40474067
| Self::Impl(Impl { generics, .. }) => Some(generics),
4048-
_ => None,
4068+
4069+
Self::ExternCrate(..)
4070+
| Self::Use(..)
4071+
| Self::Static(..)
4072+
| Self::ConstBlock(..)
4073+
| Self::Mod(..)
4074+
| Self::ForeignMod(..)
4075+
| Self::GlobalAsm(..)
4076+
| Self::MacCall(..)
4077+
| Self::MacroDef(..)
4078+
| Self::Delegation(..)
4079+
| Self::DelegationMac(..) => None,
40494080
}
40504081
}
40514082
}

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ macro_rules! common_visitor_and_walkers {
425425
ByRef,
426426
Closure,
427427
Const,
428+
ConstBlockItem,
428429
ConstItem,
429430
ConstItemRhs,
430431
Defaultness,
@@ -825,6 +826,8 @@ macro_rules! common_visitor_and_walkers {
825826
visit_visitable!($($mut)? vis, use_tree),
826827
ItemKind::Static(item) =>
827828
visit_visitable!($($mut)? vis, item),
829+
ItemKind::ConstBlock(item) =>
830+
visit_visitable!($($mut)? vis, item),
828831
ItemKind::Const(item) =>
829832
visit_visitable!($($mut)? vis, item),
830833
ItemKind::Mod(safety, ident, mod_kind) =>

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
205205
| ItemKind::Use(..)
206206
| ItemKind::Static(..)
207207
| ItemKind::Const(..)
208+
| ItemKind::ConstBlock(..)
208209
| ItemKind::Mod(..)
209210
| ItemKind::ForeignMod(..)
210211
| ItemKind::GlobalAsm(..)
@@ -282,8 +283,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
282283
self.lower_define_opaque(hir_id, define_opaque);
283284
hir::ItemKind::Static(*m, ident, ty, body_id)
284285
}
285-
ItemKind::Const(box ast::ConstItem {
286-
ident, generics, ty, rhs, define_opaque, ..
286+
ItemKind::Const(box ConstItem {
287+
defaultness: _,
288+
ident,
289+
generics,
290+
ty,
291+
rhs,
292+
define_opaque,
287293
}) => {
288294
let ident = self.lower_ident(*ident);
289295
let (generics, (ty, rhs)) = self.lower_generics(
@@ -302,6 +308,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
302308
self.lower_define_opaque(hir_id, &define_opaque);
303309
hir::ItemKind::Const(ident, generics, ty, rhs)
304310
}
311+
ItemKind::ConstBlock(ConstBlockItem { span, id, block }) => hir::ItemKind::Const(
312+
self.lower_ident(ConstBlockItem::IDENT),
313+
hir::Generics::empty(),
314+
self.arena.alloc(self.ty_tup(DUMMY_SP, &[])),
315+
hir::ConstItemRhs::Body({
316+
let body = hir::Expr {
317+
hir_id: self.lower_node_id(*id),
318+
kind: hir::ExprKind::Block(self.lower_block(block, false), None),
319+
span: self.lower_span(*span),
320+
};
321+
self.record_body(&[], body)
322+
}),
323+
),
305324
ItemKind::Fn(box Fn {
306325
sig: FnSig { decl, header, span: fn_sig_span },
307326
ident,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
537537
gate_all!(super_let, "`super let` is experimental");
538538
gate_all!(frontmatter, "frontmatters are experimental");
539539
gate_all!(coroutines, "coroutine syntax is experimental");
540+
gate_all!(const_block_items, "const block items are experimental");
540541

541542
if !visitor.features.never_patterns() {
542543
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ impl<'a> State<'a> {
205205
define_opaque.as_deref(),
206206
);
207207
}
208+
ast::ItemKind::ConstBlock(ast::ConstBlockItem { id: _, span: _, block }) => {
209+
let ib = self.ibox(INDENT_UNIT);
210+
self.word("const");
211+
self.nbsp();
212+
{
213+
let cb = self.cbox(0);
214+
let ib = self.ibox(0);
215+
self.print_block_with_attrs(block, &[], cb, ib);
216+
}
217+
self.end(ib);
218+
}
208219
ast::ItemKind::Const(box ast::ConstItem {
209220
defaultness,
210221
ident,

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ fn parse_cfg_attr_internal<'a>(
360360
) -> PResult<'a, (CfgEntry, Vec<(ast::AttrItem, Span)>)> {
361361
// Parse cfg predicate
362362
let pred_start = parser.token.span;
363-
let meta = MetaItemOrLitParser::parse_single(parser, ShouldEmit::ErrorsAndLints)?;
363+
let meta =
364+
MetaItemOrLitParser::parse_single(parser, ShouldEmit::ErrorsAndLints { recover: true })?;
364365
let pred_span = pred_start.with_hi(parser.token.span.hi());
365366

366367
let cfg_predicate = AttributeParser::parse_single_args(
@@ -375,7 +376,7 @@ fn parse_cfg_attr_internal<'a>(
375376
CRATE_NODE_ID,
376377
Target::Crate,
377378
features,
378-
ShouldEmit::ErrorsAndLints,
379+
ShouldEmit::ErrorsAndLints { recover: true },
379380
&meta,
380381
parse_cfg_entry,
381382
&CFG_ATTR_TEMPLATE,

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ pub fn parse_cfg_select(
7878
}
7979
}
8080
} else {
81-
let meta = MetaItemOrLitParser::parse_single(p, ShouldEmit::ErrorsAndLints)
82-
.map_err(|diag| diag.emit())?;
81+
let meta =
82+
MetaItemOrLitParser::parse_single(p, ShouldEmit::ErrorsAndLints { recover: true })
83+
.map_err(|diag| diag.emit())?;
8384
let cfg_span = meta.span();
8485
let cfg = AttributeParser::parse_single_args(
8586
sess,
@@ -94,7 +95,7 @@ pub fn parse_cfg_select(
9495
// Doesn't matter what the target actually is here.
9596
Target::Crate,
9697
features,
97-
ShouldEmit::ErrorsAndLints,
98+
ShouldEmit::ErrorsAndLints { recover: true },
9899
&meta,
99100
parse_cfg_entry,
100101
&AttributeTemplate::default(),

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl Stage for Late {
381381
}
382382

383383
fn should_emit(&self) -> ShouldEmit {
384-
ShouldEmit::ErrorsAndLints
384+
ShouldEmit::ErrorsAndLints { recover: true }
385385
}
386386
}
387387

@@ -438,7 +438,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
438438
pub(crate) fn emit_lint(&mut self, lint: &'static Lint, kind: AttributeLintKind, span: Span) {
439439
if !matches!(
440440
self.stage.should_emit(),
441-
ShouldEmit::ErrorsAndLints | ShouldEmit::EarlyFatal { also_emit_lints: true }
441+
ShouldEmit::ErrorsAndLints { .. } | ShouldEmit::EarlyFatal { also_emit_lints: true }
442442
) {
443443
return;
444444
}
@@ -765,9 +765,18 @@ pub enum ShouldEmit {
765765
EarlyFatal { also_emit_lints: bool },
766766
/// The operation will emit errors and lints.
767767
/// This is usually what you need.
768-
ErrorsAndLints,
769-
/// The operation will emit *not* errors and lints.
770-
/// Use this if you are *sure* that this operation will be called at a different time with `ShouldEmit::ErrorsAndLints`.
768+
ErrorsAndLints {
769+
/// Whether [`ArgParser`] will attempt to recover from errors.
770+
///
771+
/// If true, it will attempt to recover from bad input (like an invalid literal). Setting
772+
/// this to false will instead return early, and not raise errors except at the top level
773+
/// (in [`ArgParser::from_attr_args`]).
774+
recover: bool,
775+
},
776+
/// The operation will *not* emit errors and lints.
777+
///
778+
/// The parser can still call `delay_bug`, so you *must* ensure that this operation will also be
779+
/// called with `ShouldEmit::ErrorsAndLints`.
771780
Nothing,
772781
}
773782

@@ -776,7 +785,7 @@ impl ShouldEmit {
776785
match self {
777786
ShouldEmit::EarlyFatal { .. } if diag.level() == Level::DelayedBug => diag.emit(),
778787
ShouldEmit::EarlyFatal { .. } => diag.upgrade_to_fatal().emit(),
779-
ShouldEmit::ErrorsAndLints => diag.emit(),
788+
ShouldEmit::ErrorsAndLints { .. } => diag.emit(),
780789
ShouldEmit::Nothing => diag.delay_as_bug(),
781790
}
782791
}

0 commit comments

Comments
 (0)