Pre-RFC: remove some macros from std prelude

The std prelude contains quite a number of macros, not all of them frequently used. I believe the reason for this is that prior to Edition 2018 there wasn't a good way to import macros on an as-needed basis.

Some of these macros are widely used and should remain in the prelude: println!, assert!, #[cfg(..)], #[derive(Debug)], vec!.

Other macros are less frequently used, but at least are unlikely to be confused with anything else: include_bytes!, stringify!.

A few are potentially a bit more confusing and not commonly used: file!, column!, env!.

In particular, column! may be confused with e.g. iced::widget::column! (I am planning to introduce a macro by the same to KAS). As a consequence of this existing in the std prelude, attempting to use the widget-constructing macro without a matching import yields a slightly confusing error: error: column! takes no arguments, while importing the latter macro with a glob-import is insufficient (ambiguous usage).

Proposal:

  1. Pick a set of confusing rarely used macros for removal from the std prelude.
  2. Deprecate usage of these macros through the prelude.
  3. From Edition 2024 (or a later edition if necessary) remove these from the prelude.
1 Like

These macros technically aren't in the prelude currently. Rather, they're made available by the fact std (or core) is injected with a #[macro_use].

I previously filed an ACP to make them also available in the proper prelude, which was accepted, but haven't gotten around to making a PR to do so yet.

It's a conflict, yes, but I don't see why std is the one who should change. There definitely are uses of std::column! in the wild -- some prefixed like that, but many not: github search

This is a macro that has been available since Rust 1.0. (And before that, in 0.12 it was called col!) IMO the onus is on iced and others to differentiate if the overlap is causing problems.

We should absolutely be cautious about adding new macros in privileged prelude/#[macro_use] positions, but I see no value in churning the old ones out.

4 Likes

I find the argument weird. Not only has the std macros existed since 1.0, iced isn't even some widely used or foundational dependency. Why should std change to accommodate it? The macros you listed aren't very common, but neither are they something super obscure. I've definitely used file! and column!, for example, in various debug macros.

That said, the diagnostics could indeed be improved. For example, the error could use a fully qualified name ::std::column! instead of just column!, or suggest using the iced::widget::column! macro (the latter can be difficult to implement, since in general macros don't provide information on the permissible syntax of their contents).

3 Likes

One relevant issue: Do not apply `#[macro_use]` to implicitly injected `extern crate std;`, use standard library prelude instead · Issue #53977 · rust-lang/rust · GitHub

1 Like

See also RFC 3365: Re-export stdlib macros from submodules.

Thanks folks. I agree that std should have precedence in case of conflict. On the other hand, I don't know that column! without prefix is used all that much to refer to std::column! (in @cuviper's search above, at least half the actual usages appear to be for some form of UI, not position in the source file, while a number just appear to be listings of macros).

I'm more concerned about confusion and agree that an improved diagnostic could help here.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.