Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse exceptions during reloads #366

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

9999years
Copy link
Member

The reload summary ("Ok, 10 modules loaded") is still responsible for whether or not "All good!" is printed, so this still needs some work. We could just check if any exceptions (or errors?) are printed and mark it as a compilation failure if so?

The reload summary ("Ok, 10 modules loaded") is still responsible for
whether or not "All good!" is printed, so this still needs some work. We
could just check if any exceptions (or errors?) are printed and mark it
as a compilation failure if so?
Copy link

linear bot commented Mar 13, 2025

@github-actions github-actions bot added the patch Bug fixes or non-functional changes label Mar 13, 2025
@9999years 9999years requested a review from lf- March 13, 2025 20:31
pub fn exception(input: &mut &str) -> PResult<GhcMessage> {
let _ = "*** Exception: ".parse_next(input)?;

let message = until_newline.parse_next(input)?;
Copy link
Member

@evanrelf evanrelf Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: I realized you already know this in the comment below this.

I think the most correct parser of a GHCi exception would parse until a prompt, not a newline. But until a newline would handle this particular exception, if that's all you're going for right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the issue is we get these in a stream of arbitrary GHC output, which may include other diagnostics we want to parse, compiling-such-and-such-a-module messages, etc.

Comment on lines +40 to +52
// Doesn't parse subsequent lines (even if they're relevant, unfortunately).
assert_eq!(
exception
.parse(indoc!(
"
*** Exception: puppy doggy
CallStack (from HasCallStack):
error, called at <interactive>:3:1 in interactive:Ghci1
"
))
.unwrap(),
GhcMessage::Exception("puppy doggy".into())
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay you already know that. Glad to see it still works in the presence of multi-line exception messages?

/// ```text
/// *** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
/// ```
Exception(String),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't plan to mutate this string, you could use less memory:

Suggested change
Exception(String),
Exception(Box<str>),

If you think GhcMessages will be cloned a lot, you could account for that too:

Suggested change
Exception(String),
Exception(Arc<str>),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like kinda a yak shave imo and the ergonomics of dealing with both of those types are worse. it's pretty regular to just put String places that it doesn't strictly have to be because it's easy. if the memory usage starts mattering it can be changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm pretty sure zero-copy parsing is kind of an ergonomic nightmare, even if it's technically possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ergonomics of dealing with both of those types are worse

Not true. All of the &str API is still available thanks to the Deref impl for Box<T> and Arc<T>.

zero-copy parsing is kind of an ergonomic nightmare

This is not zero-copy, and this would not require adding lifetimes.


I don't feel strongly about which type you choose, but I do wanna make sure you understand the tradeoffs correctly! I am not suggesting any ergonomic sacrifice here!

This video explains things better: https://www.youtube.com/watch?v=A4cKi7PTJSs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's all you'd need to change:

Diff
diff --git a/src/ghci/parse/ghc_message/exception.rs b/src/ghci/parse/ghc_message/exception.rs
index b09906d..55f4222 100644
--- a/src/ghci/parse/ghc_message/exception.rs
+++ b/src/ghci/parse/ghc_message/exception.rs
@@ -1,3 +1,4 @@
+use std::sync::Arc;
 use winnow::PResult;
 use winnow::Parser;
 
@@ -15,7 +16,7 @@ pub fn exception(input: &mut &str) -> PResult<GhcMessage> {
 
     let message = until_newline.parse_next(input)?;
 
-    Ok(GhcMessage::Exception(message.to_owned()))
+    Ok(GhcMessage::Exception(Arc::from(message)))
 }
 
 #[cfg(test)]
@@ -29,12 +30,12 @@ mod tests {
     fn test_parse_exception() {
         assert_eq!(
             exception.parse("*** Exception: Uh oh!\n").unwrap(),
-            GhcMessage::Exception("Uh oh!".into())
+            GhcMessage::Exception(Arc::new("Uh oh!"))
         );
 
         assert_eq!(
             exception.parse("*** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)\n").unwrap(),
-            GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".into())
+            GhcMessage::Exception(Arc::new("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)"))
         );
 
         // Doesn't parse subsequent lines (even if they're relevant, unfortunately).
@@ -48,7 +49,7 @@ mod tests {
                     "
                 ))
                 .unwrap(),
-            GhcMessage::Exception("puppy doggy".into())
+            GhcMessage::Exception(Arc::new("puppy doggy"))
         );
     }
 }
diff --git a/src/ghci/parse/ghc_message/mod.rs b/src/ghci/parse/ghc_message/mod.rs
index 6036a1d..34e2692 100644
--- a/src/ghci/parse/ghc_message/mod.rs
+++ b/src/ghci/parse/ghc_message/mod.rs
@@ -1,6 +1,7 @@
 //! Parser for GHC compiler output.
 
 use std::fmt::Display;
+use std::sync::Arc;
 
 use camino::Utf8PathBuf;
 use miette::miette;
@@ -75,7 +76,7 @@ pub enum GhcMessage {
     /// ```text
     /// *** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
     /// ```
-    Exception(String),
+    Exception(Arc<str>),
     /// A configuration file being loaded.
     ///
     /// ```text
@@ -336,7 +337,7 @@ mod tests {
                         ].join("\n"),
                     },
                 ),
-                GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".to_owned()),
+                GhcMessage::Exception(Arc::new("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)")),
                 GhcMessage::Summary(
                     CompilationSummary {
                         result: CompilationResult::Err,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Bug fixes or non-functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants