Skip to content

Commit bfd5d59

Browse files
committed
Prevent impossible combinations in ast::ModKind.
`ModKind::Loaded` has an `inline` field and a `had_parse_error` field. If the `inline` field is `Inline::Yes` then `had_parse_error` must be `Ok(())`. This commit moves the `had_parse_error` field into the `Inline::No` variant. This makes it impossible to create the nonsensical combination of `inline == Inline::Yes` and `had_parse_error = Err(_)`.
1 parent 425a9c0 commit bfd5d59

File tree

17 files changed

+28
-25
lines changed

17 files changed

+28
-25
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,7 +3137,7 @@ impl FnRetTy {
31373137
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, Walkable)]
31383138
pub enum Inline {
31393139
Yes,
3140-
No,
3140+
No { had_parse_error: Result<(), ErrorGuaranteed> },
31413141
}
31423142

31433143
/// Module item kind.
@@ -3147,7 +3147,7 @@ pub enum ModKind {
31473147
/// or with definition outlined to a separate file `mod foo;` and already loaded from it.
31483148
/// The inner span is from the first token past `{` to the last token until `}`,
31493149
/// or from the first to the last token in the loaded file.
3150-
Loaded(ThinVec<Box<Item>>, Inline, ModSpans, Result<(), ErrorGuaranteed>),
3150+
Loaded(ThinVec<Box<Item>>, Inline, ModSpans),
31513151
/// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
31523152
Unloaded,
31533153
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
251251
ItemKind::Mod(_, ident, mod_kind) => {
252252
let ident = self.lower_ident(*ident);
253253
match mod_kind {
254-
ModKind::Loaded(items, _, spans, _) => {
254+
ModKind::Loaded(items, _, spans) => {
255255
hir::ItemKind::Mod(ident, self.lower_mod(items, spans))
256256
}
257257
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11691169
self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });
11701170
}
11711171
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1172-
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _))
1172+
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _))
11731173
&& !attr::contains_name(&item.attrs, sym::path)
11741174
{
11751175
self.check_mod_file_item_asciionly(*ident);

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
141141
if let ast::ItemKind::Mod(
142142
_,
143143
_,
144-
ModKind::Loaded(.., ast::ModSpans { inner_span: span, .. }, _),
144+
ModKind::Loaded(.., ast::ModSpans { inner_span: span, .. }),
145145
) = item.kind
146146
{
147147
let prev_tests = mem::take(&mut self.tests);

compiler/rustc_expand/src/expand.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
799799
ItemKind::Mod(
800800
_,
801801
_,
802-
ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _, _),
802+
ModKind::Unloaded
803+
| ModKind::Loaded(_, Inline::No { .. }, _),
803804
)
804805
) =>
805806
{
@@ -1004,7 +1005,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10041005
fn visit_item(&mut self, item: &'ast ast::Item) {
10051006
match &item.kind {
10061007
ItemKind::Mod(_, _, mod_kind)
1007-
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _)) =>
1008+
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
10081009
{
10091010
feature_err(
10101011
self.sess,
@@ -1315,7 +1316,7 @@ impl InvocationCollectorNode for Box<ast::Item> {
13151316
let ItemKind::Mod(_, ident, ref mut mod_kind) = node.kind else { unreachable!() };
13161317
let ecx = &mut collector.cx;
13171318
let (file_path, dir_path, dir_ownership) = match mod_kind {
1318-
ModKind::Loaded(_, inline, _, _) => {
1319+
ModKind::Loaded(_, inline, _) => {
13191320
// Inline `mod foo { ... }`, but we still need to push directories.
13201321
let (dir_path, dir_ownership) = mod_dir_path(
13211322
ecx.sess,
@@ -1329,7 +1330,7 @@ impl InvocationCollectorNode for Box<ast::Item> {
13291330
// This lets `parse_external_mod` catch cycles if it's self-referential.
13301331
let file_path = match inline {
13311332
Inline::Yes => None,
1332-
Inline::No => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path),
1333+
Inline::No { .. } => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path),
13331334
};
13341335
node.attrs = attrs;
13351336
(file_path, dir_path, dir_ownership)
@@ -1365,7 +1366,7 @@ impl InvocationCollectorNode for Box<ast::Item> {
13651366
);
13661367
}
13671368

1368-
*mod_kind = ModKind::Loaded(items, Inline::No, spans, had_parse_error);
1369+
*mod_kind = ModKind::Loaded(items, Inline::No { had_parse_error }, spans);
13691370
node.attrs = attrs;
13701371
if node.attrs.len() > old_attrs_len {
13711372
// If we loaded an out-of-line module and added some inner attributes,

compiler/rustc_expand/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub(crate) fn mod_dir_path(
120120

121121
(dir_path, dir_ownership)
122122
}
123-
Inline::No => {
123+
Inline::No { .. } => {
124124
// FIXME: This is a subset of `parse_external_mod` without actual parsing,
125125
// check whether the logic for unloaded, loaded and inline modules can be unified.
126126
let file_path = mod_file_path(sess, ident, attrs, &module.dir_path, dir_ownership)

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3097,7 +3097,7 @@ impl EarlyLintPass for SpecialModuleName {
30973097
if let ast::ItemKind::Mod(
30983098
_,
30993099
ident,
3100-
ast::ModKind::Unloaded | ast::ModKind::Loaded(_, ast::Inline::No, _, _),
3100+
ast::ModKind::Unloaded | ast::ModKind::Loaded(_, ast::Inline::No { .. }, _),
31013101
) = item.kind
31023102
{
31033103
if item.attrs.iter().any(|a| a.has_name(sym::path)) {

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a> Parser<'a> {
4343
self.expect(exp!(OpenBrace))?;
4444
let (inner_attrs, items, inner_span) = self.parse_mod(exp!(CloseBrace))?;
4545
attrs.extend(inner_attrs);
46-
ModKind::Loaded(items, Inline::Yes, inner_span, Ok(()))
46+
ModKind::Loaded(items, Inline::Yes, inner_span)
4747
};
4848
Ok(ItemKind::Mod(safety, ident, mod_kind))
4949
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::Arc;
1111
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1212
use rustc_ast::{
1313
self as ast, AssocItem, AssocItemKind, Block, ConstItem, Delegation, Fn, ForeignItem,
14-
ForeignItemKind, Item, ItemKind, NodeId, StaticItem, StmtKind, TyAlias,
14+
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TyAlias,
1515
};
1616
use rustc_attr_parsing as attr;
1717
use rustc_attr_parsing::AttributeParser;
@@ -801,7 +801,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
801801
ItemKind::Mod(_, ident, ref mod_kind) => {
802802
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
803803

804-
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
804+
if let ast::ModKind::Loaded(_, Inline::No { had_parse_error: Err(_) }, _) = mod_kind
805+
{
805806
self.r.mods_with_parse_errors.insert(def_id);
806807
}
807808
self.parent_scope.module = self.r.new_local_module(

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
34343434

34353435
fn visit_item(&mut self, item: &'tcx ast::Item) {
34363436
if self.target_module == item.id {
3437-
if let ItemKind::Mod(_, _, ModKind::Loaded(items, _inline, mod_spans, _)) = &item.kind {
3437+
if let ItemKind::Mod(_, _, ModKind::Loaded(items, _inline, mod_spans)) = &item.kind {
34383438
let inject = mod_spans.inject_use_span;
34393439
if is_span_suitable_for_use_injection(inject) {
34403440
self.first_legal_span = Some(inject);

0 commit comments

Comments
 (0)