fix(ast-engine): panic if from_path is not implemented#98
Conversation
…` trait
This modifies the default trait implementation of `from_path` in `crates/ast-engine/src/language.rs` to panic with `unimplemented!("from_path is not implemented")` rather than silently returning `None`. This enforces proper implementation by types implementing the `Language` trait and prevents silent failures.
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates the default implementation of the Language::from_path method so that any Language implementor that relies on the default and calls from_path will now panic via unimplemented!() instead of silently returning None. Class diagram for updated Language trait default from_path implementationclassDiagram
class Language {
<<trait>>
+clone() Language
+kind_to_id(kind str) u16
+display_name() str
+supports_file(path Path) bool
+from_path(path Path) Option~Language~
}
note for Language "from_path now has a default implementation that panics via unimplemented instead of returning None when not overridden by implementors"
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Instead of using
unimplemented!()as the default, consider removing the default body fromfrom_pathso that implementors are forced at compile time to provide an implementation rather than encountering a runtime panic. - If you keep the panic-based default, it would be more actionable to include the type name (e.g., via
std::any::type_name::<Self>()) in the message so it's clear whichLanguageimplementation is missingfrom_path. - For types that intentionally do not support
from_path, it may be clearer to have them explicitly overridefrom_path(e.g., to returnNoneor a tailored panic) instead of relying on a genericunimplemented!()default.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of using `unimplemented!()` as the default, consider removing the default body from `from_path` so that implementors are forced at compile time to provide an implementation rather than encountering a runtime panic.
- If you keep the panic-based default, it would be more actionable to include the type name (e.g., via `std::any::type_name::<Self>()`) in the message so it's clear which `Language` implementation is missing `from_path`.
- For types that intentionally do not support `from_path`, it may be clearer to have them explicitly override `from_path` (e.g., to return `None` or a tailored panic) instead of relying on a generic `unimplemented!()` default.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR tightens the thread_ast_engine::language::Language trait contract by making the default Language::from_path implementation panic, preventing silent None returns when an implementor forgets to provide a concrete implementation.
Changes:
- Replaced the default
Language::from_pathbody fromNonetounimplemented!("from_path is not implemented").
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
crates/ast-engine/src/language.rs
Outdated
| @@ -69,8 +69,7 @@ pub trait Language: Clone + std::fmt::Debug + Send + Sync + 'static { | |||
| } | |||
| /// Return the file language from path. Return None if the file type is not supported. | |||
There was a problem hiding this comment.
The doc comment says from_path returns None when the file type is unsupported, but the default implementation now panics via unimplemented!(). Update the documentation to reflect that the default implementation panics and that implementors should override it to return None for unsupported paths (if that’s the intended contract).
| /// Return the file language from path. Return None if the file type is not supported. | |
| /// Return the file language inferred from a filesystem path. | |
| /// | |
| /// The *default* implementation is not implemented and will panic if called. | |
| /// Implementors should override this method and return `Some(Self)` when the | |
| /// file type is supported and `None` when it is not. |
There was a problem hiding this comment.
Updated the docstring to reflect that the default implementation panics via unimplemented!() and must be overridden.
crates/ast-engine/src/language.rs
Outdated
| fn from_path<P: AsRef<Path>>(_path: P) -> Option<Self> { | ||
| // TODO: throw panic here if not implemented properly? | ||
| None | ||
| unimplemented!("from_path is not implemented") |
There was a problem hiding this comment.
The panic message "from_path is not implemented" is likely to be hard to diagnose in downstream crates with multiple Language impls. Consider including the concrete Self type name and/or guidance (e.g., "override Language::from_path for this type") to make the failure actionable.
| unimplemented!("from_path is not implemented") | |
| unimplemented!( | |
| "Language::from_path is not implemented for type `{}`. \ | |
| Override Language::from_path for this type if path-based detection is required.", | |
| std::any::type_name::<Self>() | |
| ) |
There was a problem hiding this comment.
Excellent point! Improved the panic message to include the dynamic type using std::any::type_name::<Self>().
| fn from_path<P: AsRef<Path>>(_path: P) -> Option<Self> { | ||
| // TODO: throw panic here if not implemented properly? | ||
| None | ||
| unimplemented!("from_path is not implemented") | ||
| } |
There was a problem hiding this comment.
This changes Language::from_path from returning None by default to panicking, but there’s no regression test capturing the new contract. Add a unit test that asserts calling from_path on a Language impl that doesn’t override it will panic (and optionally that overriding implementations still work).
There was a problem hiding this comment.
Added tests verifying the panic string matches the expected message and confirming that explicit overrides return Some(Self) safely.
The previous run failed because of a `clippy::collapsible_if` warning due to the `-D warnings` setup in GitHub Actions. Replaced nested if block with an `&&` condition in `crates/flow/src/incremental/analyzer.rs` to fix this issue. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Fixed misspelled words in `_typos.toml` to prevent CI failures. 1. Replaced "inout" with "input" in `classifications/_universal_rules.json`. 2. Appended "inout", "ba", "ede", and "Bare" to `_typos.toml` `extend-ignore-identifiers-re` config since they were used as valid references/names in other locations. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
…failure on stable toolchains
The usage of the nightly `#![feature(trait_alias)]` caused build failures on standard stable release channels used across standard checks. Replaced the `Doc` trait alias with a regular trait definition `pub trait Doc: Clone + 'static {}` and a blanket implementation to remain compliant with stable constraints.
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
|
@copilot open a new pull request to apply changes based on the comments in this thread and this one |
|
@bashandbone I've opened a new pull request, #102, to work on those changes. Once the pull request is ready, I'll request review from you. |
…ult panic behavior Added unit tests to `crates/ast-engine/src/language.rs` to confirm the panic message format correctly identifies the missing implementation type name and verify an explicit override safely succeeds. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
…ics and contract tests (#102) * Initial plan * fix(ast-engine): improve Language::from_path doc, panic message, and add tests Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> --------- Signed-off-by: Adam Poulemanos <89049923+bashandbone@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Language::from_pathincrates/ast-engine/src/language.rsto useunimplemented!().Languagethat attempts to usefrom_pathwithout providing a concrete implementation will panic.thread-ast-enginelibrary crates, avoiding issues withNonereturning silently.PR created automatically by Jules for task 12198073203083672702 started by @bashandbone
Summary by Sourcery
Bug Fixes:
Language::from_pathimplementation panic when it is not overridden.