How can I emit a warning message at compile time [duplicate] - rust

I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints. My current use case is feature deprecation, but there's other possible uses for this.

This currently isn't possible in stable Rust. However, there is an unstable feature, procedural macro diagnostics, which provides this functionality for procedural macros, via the Diagnostic API.
To emit a compiler warning from inside a procedural macro, you would use it like this:
#![feature(proc_macro_diagnostic)]
use proc_macro::Diagnostic;
Diagnostic::new()
.warning("This method is deprecated")
.emit();
To associate the warning with a specific token span, you'd use spanned_warning instead. This makes the warning output show the relevant source tokens underlined along with the message.

Related

In Flow NPM packages, what's the proper way to suppress issues so user apps don't see errors?

If you use something like $FlowIssue it's not guaranteed to be in everyone's .flowconfig file. If you declare a library interface, that seems to only work for the given project, not in other projects that import your package (even if you provide the .flowconfig and interface files in your NPM package).
Here's the code I'm trying to suppress errors for in apps that use my package:
// $FlowIssue
const isSSRTest = process.env.NODE_ENV === 'test' // $FlowIssue
&& typeof CONFIG !== 'undefined' && CONFIG.isSSR
CONFIG is a global that exists when tests are run by Jest.
I previously had an interface declaration for CONFIG, but that wasn't honored in user applications--perhaps I'm missing a mechanism to make that work?? With this solution, at least there is a good chance that user's have the $FlowIssue suppression comment. It's still not good enough though.
What's the idiomatic solution here for packages built with Flow?
Declaring a global variable
This is the way to declare a global variable:
declare var CONFIG: any;. Instead of any you could/should use the actual type.
Error Suppression
With flow v0.33 they introduced this change:
suppress_comment now defaults to matching // $FlowFixMe if there are
no suppress_comments listed in a .flowconfig
This means that there is a greater chance of your error being suppressed if you use $FlowFixMe.
Differences in .flowconfig between your library and your consumers' code are a real issue, and there is no way to make it so that your code can be dropped into any other project and be sure it will typecheck. On top of that, even if you have identical .flowconfigs, you may be running different versions of Flow. Your code may typecheck in one version, but not in another, so it may be the case that consumers of your library will be pinned to a specific version of Flow if they want to avoid getting errors reported from your library.
Worse, if one library type checks only in one version of Flow, and another type checks only in another version, there may be no version of Flow that a consumer can choose in order to avoid errors.
The only way to solve this generally is to write library definition files and publish them to flow-typed. Unfortunately, this is currently a manual process because there is not yet any tooling that can take a project and generate library definitions for it. In the mean time, simply copying your source files to have the .js.flow extension before publishing will work in some cases, but it is not a general solution.
See also https://stackoverflow.com/a/43852211/901387

How to quiet a warning for a single statement in Rust?

Say there is a single warning such as path_statements, unused_variables. Is there a way to ignore a single instant of this, without isolating them into a code block or function?
To be clear, when there is a single warning in the code. I would like the ability to quiet only that warning, without having to do special changes addressing the particular warning.
And without this quieting warnings anywhere else, even later on in the same function.
With GCC this can be done as follows:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
/* Isolated region which doesn't raise warnings! */
this_would_raise_Wformat(args);
#pragma GCC diagnostic pop
Does Rust have the equivalent capability?
Note, am asking about the general case of how to quiet warnings.
Am aware there are ways to resolve unused var warning for eg.
To silence warnings you have to add the allow(warning_type) attribute to the affected expression or any of its parents. If you only want to silence the warning on one specific expression, you can add the attribute to that expression/statement:
fn main() {
#[allow(unused_variables)]
let not_used = 27;
#[allow(path_statements)]
std::io::stdin;
println!("hi!");
}
However, the feature of adding attributes to statements/expressions (as opposed to items, like functions) is still a bit broken. In particular, in the above code, the std::io::stdin line still triggers a warning. You can read the ongoing discussion about this feature here.
Often it is not necessary to use an attribute though. Many warnings (like unused_variables and unused_must_use) can be silenced by using let _ = as the left side of your statement. In general, any variable that starts with an underscore won't trigger unused-warnings.
If you want to silence all warnings of a kind in a module, write e.g. #![allow(dead_code)] (note the exclamation mark) at the top of the module. This will disable all warnings of this kind in the whole module. You can also call rustc with e.g. -A dead_code.
You can disable all warnings by writing #![allow(warnings)] at the top of the module.
You can insert a module (as described in the Rust book) where the specific warnings are ignored.
As Lukas said, you can also write e.g. #[allow(dead_code)] on a statement or an expression.

Duplicate protocol implementations in dependent projects

I have problems with consolidation of protocol implementation in my Elixir project. To be more specific I use Ecto and some simple project called Gold (doesn't matter much atm). The problem is, both of them (Ecto and Gold) use Poison to serialize Decimals (and implement proper Protocol).
Implementation for Ecto looks somewhat like this:
defimpl Poison.Encoder, for: Decimal do
def encode(decimal, _opts), do: <<?", Decimal.to_string(decimal)::binary, ?">>
end
During development there is a warning saying that the module is duplicated:
warning: redefining module Poison.Encoder.Decimal (current version loaded from /(...)/_build/dev/lib/gold/ebin/Elixir.Poison.Encoder.Decimal.beam)
lib/ecto/poison.ex:2
But when I try to use for instance exrm to build a release, then I get errors saying that I have duplicate_modules
===> Provider (release) failed with: {error,
{rlx_prv_assembler,
{release_script_generation_error,
systools_make,
{duplicate_modules,
[{{'Elixir.Poison.Encoder.Decimal',
gold,
"/(...)/rel/bitcoin_api/lib/gold-0.12.0/ebin"},
{'Elixir.Poison.Encoder.Decimal',
ecto,
"/(...)/rel/bitcoin_api/lib/ecto-2.0.2/ebin"}}]}}}}
How should I deal with this? The case here is I actually use my own version of Gold, so I can tamper with it to fix it asap. I know I can just add Ecto to Gold as a dependency, but that seems a bit overkill to just implement one Protocol like this. Isn't there some kind of a macro to check if a module has already been implemented?
A quick fix could be to wrap Gold's implementation in a Code.ensure_loaded?/1
unless Code.ensure_loaded?(Ecto) do
defimpl Poison.Encoder, for: Decimal do
def encode(decimal, _opts), do: <<?", Decimal.to_string(decimal)::binary, ?">>
end
end
Its a little icky but then you don't have to add Ecto, but just check if something else already pulled it in

How to document macros from a Rust compiler plugin?

I notice that compiler plugins frequently provide macros that the documentation wont even mention. They're registered and created programmatically rather than being defined in a syntax rustdoc recognizes. Naturally, no documentation can be shown.
I'm looking for a way to get around that, some way of generating documentation for a macro that doesn't exist in the crate at compile time.
I notice the syntax crate could benefit from such a thing as well. quote_item, for instance, is completely undocumented. I can't even find the code that registers it.
One possibility is to do what the compiler does: create an empty macro_rules! macro and attach documentation to that. E.g. if a crate defines foo that takes a single expression, then write something like
/// Documentation
#[macro_export]
macro_rules! foo {
($e: expr) => ({ /* syntax extension */ })
}
I notice the syntax crate could benefit from such a thing as well. quote_item, for instance, is completely undocumented. I can't even find the code that registers it.
You can search the Rust source for quote_item, which is helpful for two reasons: it gives some examples, and also allows you to track down the definition. The latter is easier using Rust's DXR instance which can search for things with quotes (i.e. can find strings), and includes various source code navigation tricks (like jump-to-definition).

enumeration values not handled in switch

When compiling my project as a 64bit target (this isn't considered a warning on 32bit target) I get this warning:
5 enumeration values not handled in switch: 'kCreated', 'kLoaded', 'kConnected'...
I have somehow managed to turn off the error/warning message numbers, so I don't know what number to suppress in my code with #pragma warn.
I need to use #pragma warn because I only want to suppress approved places in my code (turning the warning off and on again).
Bonus question: Does anyone know how to get back the error/warning numbers again?
The 64bit compiler is based on CLang, which does not use warning numbers, which is why you do not see them. The following info comes from Bruneau Babet, one of C++Builder's chief developers, in the Embarcadero forums:
How do I suppress 64bit XE3 warnings?
Warnings in clang, hence bcc64, don't have the Wxxxx numbers. Behind the
scene there is a unique id generated for each warning but it's
auto-generated and cannot be assumed to remain constant across builds.
Instead each warning has a group. Related warnings are often in the same
group. Some groups have just one warning. To disable warnings of a group
you can use "-Wno-" on the command line, or via something like
the following in code:
#pragma clang diagnostic ignored "-W<groupname>".
For example, the first warning you listed is under the group "float-equal".
So, "-Wno-float-equal" should disable that warning. And to disable the one
about an enumerator not handled in the switch you can use the following in
code:
#pragma clang diagnostic ignored "-Wswitch"
So the next obvious question is how to find out about each group. The
"-fdiagnostics-show-option" should trigger the compiler to display the
option but unfortunately the IDE does not honor that option. So you must
either use the command line to find out about the group ach warning belongs
to, or you can peek at the Warning declarations here:
https://github.com/llvm/llvm-project/tree/main/clang/include/clang/Basic
The *.td files declare the various warnings. The ones mentioned above are
https://github.com/llvm/llvm-project/tree/main/clang/include/clang/Basic/DiagnosticSemaKinds.td
Oddly, #pragma clang is still not documented on the Embarcadero DocWiki.

Resources