Several documents for Rust mention the fail! function as a way to abort execution with an error.
For example, chapter 27 of Rust By Example (accessed November 16, 2014) states:
The fail! macro can be used to generate a task failure and start unwinding its stack. While unwinding, the runtime will take care of freeing all the resources owned by the task by calling the destructor of all its objects.
However when I try to use this in my own code I get the following error:
error: macro undefined: 'fail!'
You can click "Run" on the example on the "Rust By Example" page to reproduce for yourself.
What took the place of fail in the Rust standard library?
It has been renamed to panic!, see Issue 17489 and the nightly doc
Related
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.
I have an x64 MFC application (VS2019, fresh project from the wizard and filled with lots of old project code). Debug build is working fine but the project has a problem to start up in the Release build. Should be noted that it is a big project with several different subprojects and libraries included. (EDIT: pugi xml library seems to be causing the trouble, see below)
In AfxMainWin() function the pThread and pApp pointers are created correctly, and I can for example look at the command line arguments pThread->m_lpCmdLine. Then AfxMainWin calls pThread->InitInstance(), but inside the InitInstace function the this-pointer is corrupt, which leads to a crash when the code tries to access m_lpCmdLine or any other member-variable.
EDIT1: I found out that the this-pointer was okay on the first line in InitInstance(). Then a small init function was run that destroyed the this-pointer in InitInstance. This function reads settings from an xml file using Pugi Xml library, and specifically this line is the one that destroys my this pointer:
status = ((pugi::xml_document*)get_params())->load_file(file).status;
Probable reason: The pugixml.dll is incomaptible with my release build and causes memory corruption. I have used dependency walker on the pugixml.dll, and the result was not good. This DLL seems to be built in debug mode since it adds dependencies to the debug versions of MFC libraries, such as VCRUNTIME140D.DLL - and that is not good for a release build.
I do not have access to any release version of pugixml.dll, so my solution will be to throw out the pugixml.dll and include the .cpp in my project instead, or maybe use another xml reader instead. That should do the trick!
But I have a final question for you folks, so that I learn as much as possible from this: what is the most likely cause of my memory corruption? Is it the debug DLL that is incompatible with my release build? Or is it more likely that the DLL is old (v140 toolset) and my project is toolset v142? Or is it, as suggested by #Iinspectable , the actual function call (with c-style casting of the returned pointer) that is messed up and trashing my stack?
I note that the get_params() function returns a void* pointer (void* the_params;), but that void* pointer has earlier been created using the_params = new pugi::xml_document; - so at least it is being casted to the correct type.
EDIT2: after replacing the faulty pugixml.dll file with pugixml.cpp I could debug the issue, turns out to be some pugixml-related problem. I had to post that as another issue, since it is now a very different problem.
Recently, I've been playing with Rust and gRPC using the Tonic libraries. I started looking at how to create a custom Codec and I'm scratching my head over this...
Starting with this chunk of code, I copied the MockEncoder & MockDecoder and added all the same imports used here in the test module:
https://github.com/hyperium/tonic/blob/master/tonic/src/codec/prost.rs#L133-L158
Then I got stuck on this error:
no method named `bytes` found for mutable reference `&mut tonic::codec::DecodeBuf<'_>` in the current scope
method not found in `&mut tonic::codec::DecodeBuf<'_>`rustc(E0599)
codec.rs(55, 37): method not found in `&mut tonic::codec::DecodeBuf<'_>`
The error is complaining about let out = Vec::from(buf.bytes()); because DecodeBuf doesn't implement a bytes method.
Now, for some reason, if I wrap this code I copied in a module with #[cfg(tests)] (similar to how this originally appeared in Tonic) this error goes away. I can compile and run tests without any errors.
Curious, what is #[cfg(tests)] doing that makes buf.bytes() compile and is there something I can import in order to move MockDecoder outside of this test config?
The option is #[cfg(test)] (singular), not #[cfg(tests)] (plural). This is probably not failing because it's not being compiled or run at all, since the option is never set.
I've taken over a deploy process and part of this runs Cargo Clippy which was working fine up until late last week, when I started getting this error:
error: Question mark operator is useless here
I've read the suggested link https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
and understand that the ? is no longer required, and have had a look at the suggested changes it offers. I just want to know whether it is okay for me to make the changes as suggested, as some seem to remove some code so I'm not sure whether the result will be the same.
Some of the suggested changes seem to be simple and okay:
From this
Ok(dsl::orgs
.filter(dsl::salesforce_id.eq(salesforce_id))
.get_result(conn)?)
To this:
dsl::orgs
.filter(dsl::salesforce_id.eq(salesforce_id))
.get_result(conn)
So I'm guessing that the above type of change is safe to accept?
Then I have these
Here the 'optional' has disappeared in the suggested fix. From:
Ok(dsl::orgs
.filter(
dsl::customer_id
.eq(new_org.customer_id)
.and(dsl::name.eq(&new_org.name)),
)
.get_result(conn)
.optional()?)
To
dsl::orgs
.filter(
dsl::customer_id
.eq(new_org.customer_id)
.and(dsl::name.eq(&new_org.name)),
)
And this one, where the inner join has disappeared in the suggested fix:
Ok(orgs::dsl::orgs
.filter(orgs::dsl::customer_id.eq(customer_id))
.filter(orgs::dsl::kind.eq(org_kind))
.filter(
orgs::dsl::kind
.eq(OrgKind::Production)
.or(orgs::dsl::name.eq(&org_name)),
)
.inner_join(schema::customers::dsl::customers)
.get_result(conn)?)
to this:
orgs::dsl::orgs
.filter(orgs::dsl::customer_id.eq(customer_id))
.filter(orgs::dsl::kind.eq(org_kind))
.filter(
orgs::dsl::kind
.eq(OrgKind::Production)
Are these 2 suggested fixes okay to implement? Could someone please provide some help?
I've taken over a deploy process and part of this runs Cargo Clippy which was working fine up until late last week,
The lint appeared last week because Rust 1.51 and a new accompanying Clippy version was released last Thursday, and it added needless_question_mark.
when I started getting this error:
Your CI should probably not consider all Clippy lints to be errors. Clippy is designed to give warnings about many things that are simply code style improvements, or possible problems; counting them as errors will lead to unnecessary breakage in the future.
So I'm guessing that the above type of change is safe to accept?
Ok(some_expression?) is almost the same as some_expression. The only difference is that the version with ? may implicitly convert the error type of the expression (the E in Result<T, E>) to the error type expected by the function. If the error types are the same and no conversion is required, then you can remove the Ok and ? and get the same results; if the error types are different, the simplified version will not compile.
Here the 'optional' has disappeared in the suggested fix.
This seems like a bug in Clippy or whatever tool is applying the fix, as the resulting code is semantically different and would not even compile. I'd remove the Ok and ? manually in that case, and report the bug.
I swear I saw a new feature in a recent set of GHC release notes - but now I can find no reference to it. Am I delusional, or does this feature actually exist?
It was to do with loading incomplete modules. As best as I can remember, it allows you to turn off compilation errors due to undefined variables. (Naturally, at run-time this causes an exception to be thrown if you try to actually use the undefined variables for anything.) Does that sound familiar? Or is my mind making this up?
You are looking for a compile time option, vs a language extension, of "defer errors to runtime". That is, compile with -fdefer-type-errors.