about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTanishq Rajesh Jain <127596924+Tanjaint21@users.noreply.github.com>2024-03-17 23:02:41 +0530
committerGitHub <noreply@github.com>2024-03-17 13:32:41 -0400
commit202fa22cee5badff77129a7bea5c90228d354ac9 (patch)
treeb9e52ab17695a442b0275d056c84f121810e8354
parentdd301b0c04d5b5ba0fba54671bc115fc34b40862 (diff)
downloadrust-202fa22cee5badff77129a7bea5c90228d354ac9.tar.gz
rust-202fa22cee5badff77129a7bea5c90228d354ac9.zip
Fix: ICE when formatting builtins
Replace `unreachable!` with `None`. Now rustfmt won't
panic when it comes across a `builtin # offset_of` or any other builtin
-rw-r--r--src/expr.rs7
-rw-r--r--tests/rustfmt/main.rs10
-rw-r--r--tests/target/issue-5885.rs3
-rw-r--r--tests/target/issue-6105.rs1
4 files changed, 17 insertions, 4 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 147f4b31a3b..6a21d88ac9d 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -404,8 +404,11 @@ pub(crate) fn format_expr(
         ast::ExprKind::FormatArgs(..)
         | ast::ExprKind::IncludedBytes(..)
         | ast::ExprKind::OffsetOf(..) => {
-            // These do not occur in the AST because macros aren't expanded.
-            unreachable!()
+            // These don't normally occur in the AST because macros aren't expanded. However,
+            // rustfmt tries to parse macro arguments when formatting macros, so it's not totally
+            // impossible for rustfmt to come across one of these nodes when formatting a file.
+            // Also, rustfmt might get passed the output from `-Zunpretty=expanded`.
+            None
         }
         ast::ExprKind::Err => None,
     };
diff --git a/tests/rustfmt/main.rs b/tests/rustfmt/main.rs
index 11fb4786e82..39c93c97fa6 100644
--- a/tests/rustfmt/main.rs
+++ b/tests/rustfmt/main.rs
@@ -176,8 +176,14 @@ fn rustfmt_emits_error_on_line_overflow_true() {
 #[test]
 #[allow(non_snake_case)]
 fn dont_emit_ICE() {
-    let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs", "tests/target/issue_6069.rs"];
-
+    let files = [
+        "tests/target/issue_5728.rs",
+        "tests/target/issue_5729.rs",
+        "tests/target/issue-5885.rs",
+        "tests/target/issue_6069.rs",
+        "tests/target/issue-6105.rs",
+    ];
+    
     for file in files {
         let args = [file];
         let (_stdout, stderr) = rustfmt(&args);
diff --git a/tests/target/issue-5885.rs b/tests/target/issue-5885.rs
new file mode 100644
index 00000000000..85a659cbb47
--- /dev/null
+++ b/tests/target/issue-5885.rs
@@ -0,0 +1,3 @@
+fn main() {
+    println!("{}", builtin # offset_of(A, 0. 1.1.1));
+}
diff --git a/tests/target/issue-6105.rs b/tests/target/issue-6105.rs
new file mode 100644
index 00000000000..1eb0ce89f78
--- /dev/null
+++ b/tests/target/issue-6105.rs
@@ -0,0 +1 @@
+const _: () = builtin # offset_of(x, x);