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

Fix ancient defect and dependency complications related to having two rust-mode implementations #534

Merged
merged 2 commits into from Mar 29, 2024

Conversation

tarsius
Copy link
Contributor

@tarsius tarsius commented Mar 16, 2024

lib/rust-mode/rust-mode-treesitter.el:21:33: Warning: reference to free variable ‘rust-before-save-hook’
lib/rust-mode/rust-mode-treesitter.el:22:32: Warning: reference to free variable ‘rust-after-save-hook’

and

lib/rust-mode/rust-rustfmt.el:60:12: Warning: ‘unwind-protect’ without unwind forms

That was always the intention, but the cleanup code was always
placed outside the unwind forms.

  lib/rust-mode/rust-rustfmt.el:60:12: Warning: ‘unwind-protect’
    without unwind forms
@@ -5,6 +5,9 @@

;;; Code:

(defvar rust-before-save-hook)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this come from rust-common.el ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but it seems you forgot to explicitly require that. It appears that, for some reason, which escapes me now, I assumed there was a reason why this library doesn't require any other libraries.

Copy link
Member

Choose a reason for hiding this comment

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

But rust-mode.el does require it:

(require 'rust-common)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But rust-mode-treesitter.el does not require rust-mode or any other library.

Copy link
Member

Choose a reason for hiding this comment

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

That's because rust-mode.el requires rust-mode-treesitter.el:

(if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And that is why we need to declare this variable. When we compile rust-mode-treesitter.el, the compiler doesn't know that at runtime, rust-mode-treesitter is loaded because it is required in rust-mode, and that it is assumed the user (or some third-party code) does not load/require rust-mode-treesitter directly. So we have to tell the compiler about it, so it can stop being alarmist about it, and so that we don't get used to it outputting warnings and thus end up ignoring them.

However, I don't think that is right way to do it. This mode does depend other parts of this package, so it should depend on them using require. This is complicated by the fact that you also want to load this file, whenever rust-mode is loaded (modulo some conditions, that are not relevant to this discussion).

You can have it both ways, by moving (require 'rust-mode-treesitter) below (provide 'rust-mode) (as is already being done for (require 'rust-utils)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rust-prog-mode should also require rust-mode.

Copy link
Member

Choose a reason for hiding this comment

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

You can have it both ways, by moving (require 'rust-mode-treesitter) below (provide 'rust-mode) (as is already being done for (require 'rust-utils)).

Can you update your PR like that ? This whole require thing has caused many issues that it makes me nervous in general. Eg: #520, #527

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've pushed an update.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I did a quick testing and this it looks good. I will use it throughout the day and see if any issue pops up.

"rust-prog-mode.el" and "rust-mode-treesitter.el" provide competing
implementations of `rust-mode'.  Both implementations depend on code
in "rust-mode.el", and thus must require that.

Doing that is complicated by the fact that "rust-mode.el" loads one
of these libraries, depending on `rust-mode-treesitter-derive's value.

Address this conflict by:

1. Requiring feature `rust-mode' in the two libraries that implement the
   `rust-mode' major-mode and that use things defined in "rust-mode.el".

2. Moving the require forms for these two libraries in "rust-mode.el",
   below the `provide' form for `rust-mode'.
@tarsius tarsius changed the title Fix ancient defect and silence recent warning Fix ancient defect and dependency complications related to having two rust-mode implementations Mar 27, 2024
@psibi
Copy link
Member

psibi commented Mar 28, 2024

@jroimartin / @condy0919 Can you also test drive this PR since you have had related issues previously ?

@condy0919
Copy link
Contributor

@jroimartin / @condy0919 Can you also test drive this PR since you have had related issues previously ?

My pleasure.

@jroimartin
Copy link
Contributor

@jroimartin / @condy0919 Can you also test drive this PR since you have had related issues previously ?

@psibi it seems to work fine on my side.

;;;###autoload
(autoload 'rust-mode "rust-mode" "Major mode for Rust code." t)

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))

(provide 'rust-mode)

(if (and rust-mode-treesitter-derive
(version<= "29.1" emacs-version))
Copy link
Contributor

Choose a reason for hiding this comment

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

Emacs > 29.1 can be compiled without tree sitter feature.
We should maybe replace this with (functionp 'treesit-parser-p), or something similar ?

Copy link
Member

Choose a reason for hiding this comment

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

That's why this is opt in feature, by default rust-mode-treesiter-derive is nil.

We should maybe replace this with (functionp 'treesit-parser-p), or something similar ?

Even then it should be opt-in IMO (atleast for a couple of years) and that already introduces a new defcustom variable. Also, in my experience I have found treesit-parser-p not reliable where during startup it returned nil.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean we should replace (version<= "29.1" emacs-version) with (functionp 'treesit-parser-p).

Also, in my experience I have found treesit-parser-p not reliable where during startup it returned nil.

(functionp 'treesit-parser-p) doesn't call treesit-parser-p, it will just return nil when emacs has not been compiled with treesit (--without-tree-sitter flag at compile time)

Copy link
Member

Choose a reason for hiding this comment

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

I mean we should replace (version<= "29.1" emacs-version) with (functionp 'treesit-parser-p).

Ah, I see what you mean. Probably you can open a separate PR for it ?

Copy link
Contributor

Choose a reason for hiding this comment

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

And also the if check in rust-mode-treesitter.el

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agree. I have opened an issue to track this: #537

@psibi
Copy link
Member

psibi commented Mar 29, 2024

I'm merging this since I have seen no objections from others. Thanks @tarsius for the PR! Thanks @condy0919 and @jroimartin for the additional testing.

@psibi psibi merged commit b2b18aa into rust-lang:master Mar 29, 2024
17 checks passed
@tarsius
Copy link
Contributor Author

tarsius commented Mar 29, 2024

You're welcome!

@tarsius tarsius deleted the fixup branch March 29, 2024 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants