`url_serde` asking for unrelated trait bounds - url

Using the serde and url_serde crates, I get errors suggesting I need to fulfill unrelated trait bounds:
Compiling ser v0.1.0 (/data/scratch/ser)
error[E0277]: the trait bound `for<'a> url_serde::Ser<'a, url::Url>: a::_::_serde::Serialize` is not satisfied
--> src/b.rs:4:10
|
4 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^ the trait `for<'a> a::_::_serde::Serialize` is not implemented for `url_serde::Ser<'a, url::Url>`
|
::: /home/danj/.cargo/registry/src/github.com-1ecc6299db9ec823/url_serde-0.2.0/src/lib.rs:77:46
|
77 | where S: Serializer, for<'a> Ser<'a, T>: Serialize
| --------- required by this bound in `url_serde::serialize`
|
= help: the following implementations were found:
<url_serde::Ser<'a, std::option::Option<url::Url>> as a::_::_serde::Serialize>
<url_serde::Ser<'a, url::Url> as a::_::_serde::Serialize>
<url_serde::Ser<'a, url::host::Host<String>> as a::_::_serde::Serialize>
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `url_serde::De<url::Url>: a::_::_serde::Deserialize<'_>` is not satisfied
--> src/b.rs:4:21
|
4 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ the trait `a::_::_serde::Deserialize<'_>` is not implemented for `url_serde::De<url::Url>`
|
::: /home/danj/.cargo/registry/src/github.com-1ecc6299db9ec823/url_serde-0.2.0/src/lib.rs:158:40
|
158 | where D: Deserializer<'de>, De<T>: Deserialize<'de>
| ---------------- required by this bound in `url_serde::deserialize`
|
= help: the following implementations were found:
<url_serde::De<std::option::Option<url::Url>> as a::_::_serde::Deserialize<'de>>
<url_serde::De<url::Url> as a::_::_serde::Deserialize<'de>>
<url_serde::De<url::host::Host> as a::_::_serde::Deserialize<'de>>
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `ser`.
To learn more, run the command again with --verbose.
Cargo.toml
[package]
name = "ser"
version = "0.1.0"
authors = ["..."]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.115", features = ["derive"] }
url = "2.1.1"
url_serde = "0.2.0"
src/lib.rs:
pub mod a;
pub mod b;
src/a.rs:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct A {
pub name: String,
}
src/b.rs:
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Serialize, Deserialize)]
pub struct B {
#[serde(with = "url_serde")]
uri: Url,
}

Found this through crates.io linking to an upgrade: https://github.com/servo/rust-url/blob/b03895bd479d72c75600dc09c8c7906f5ee203ea/UPGRADING.md
tl;dr
url = { version = "2.0", features = ["serde"] }
and remove all references to url_serde

Related

Using data dependency for bazel ctx.action.run_shell custom rule

I am looking at emit_rule example in bazel source tree:
https://github.com/bazelbuild/examples/blob/5a8696429e36090a75eb6fee4ef4e91a3413ef13/rules/shell_command/rules.bzl
I want to add a data dependency to the custom rule. My understanding of dependency attributes documentation calls for data attr label_list to be used, but it does not appear to work?
# This example copied from docs
def _emit_size_impl(ctx):
in_file = ctx.file.file
out_file = ctx.actions.declare_file("%s.pylint" % ctx.attr.name)
ctx.actions.run_shell(
inputs = [in_file],
outputs = [out_file],
command = "wc -c '%s' > '%s'" % (in_file.path, out_file.path),
)
return [DefaultInfo(files = depset([out_file]),)]
emit_size = rule(
implementation = _emit_size_impl,
attrs = {
"file": attr.label(mandatory = True,allow_single_file = True,),
"data": attr.label_list(allow_files = True),
# ^^^^^^^ Above does not appear to be sufficient to copy data dependency into sandbox
},
)
With this rule emit_size(name = "my_name", file = "my_file", data = ["my_data"]) I want to see my_data copied to bazel-out/ before running the command. How do I go about doing this?
The data files should be added as inputs to the actions that need those files, e.g. something like this:
def _emit_size_impl(ctx):
in_file = ctx.file.file
out_file = ctx.actions.declare_file("%s.pylint" % ctx.attr.name)
ctx.actions.run_shell(
inputs = [in_file] + ctx.files.data,
outputs = [out_file],
# For production rules, probably should use ctx.actions.run() and
# ctx.actions.args():
# https://bazel.build/rules/lib/Args
command = "echo data is: ; %s ; wc -c '%s' > '%s'" % (
"cat " + " ".join([d.path for d in ctx.files.data]),
in_file.path, out_file.path),
)
return [DefaultInfo(files = depset([out_file]),)]
emit_size = rule(
implementation = _emit_size_impl,
attrs = {
"file": attr.label(mandatory = True, allow_single_file = True,),
"data": attr.label_list(allow_files = True),
},
)
BUILD:
load(":defs.bzl", "emit_size")
emit_size(
name = "size",
file = "file.txt",
data = ["data1.txt", "data2.txt"],
)
$ bazel build size
INFO: Analyzed target //:size (4 packages loaded, 9 targets configured).
INFO: Found 1 target...
INFO: From Action size.pylint:
data is:
this is data
this is other data
Target //:size up-to-date:
bazel-bin/size.pylint
INFO: Elapsed time: 0.323s, Critical Path: 0.02s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
$ cat bazel-bin/size.pylint
22 file.txt

How do I get the files in the build directory in another bazel rule

when use the python tool to generate the .cpp/.hpp code like the protobuf tool, but I don't know how many files will be generated, so it's a little not the same as protbuf tool.
In one genrule:
def __generate_core_ifce_impl(ctx):
...
output_file = ctx.actions.declare_directory(out)
cmd = """
mkdir -p {path};
""".format(path = output_file.path)
cmd += """
{tools} -i {src} -o {output_dir}
""".format(tools = tools, src = ctx.files.srcs, output_dir = output_file.path)
ctx.actions.run_shell(
command = cmd,
inputs = ctx.files.srcs,
outputs = [output_file]
)
return [DefaultInfo(files = depset([output_file])),]
_generate_core_ifce = rule (
implementation = __generate_core_ifce_impl,
attrs = {
"srcs": attr.label_list(mandatory = False, allow_files = True),
"tools": attr.label_list(mandatory = True, allow_files = True),
"out": attr.sting(mandatory = True),
},
)
In output_file directory , there will generate some *.cpp && *.hpp, but i can't know their names
then in another rule , cc_library will use *.cpp && *.hpp which are in output_file directory
the questions is: how to write this rule?
I can't get the files in the output_file diectory,
so I can't write the cc_library?
You should be able to use the name of the target, and the cc_library will use the files that are given in the DefaultInfo, e.g.:
_generate_core_ifce(
name = "my_generate_core_ifce_target",
...
)
cc_library(
name = "my_cc_library_target",
srcs = [":my_generate_core_ifce_target"],
...
)
edit: adding an example:
BUILD:
load(":defs.bzl", "my_rule")
my_rule(
name = "my_target",
)
cc_binary(
name = "cc",
srcs = [":my_target"],
)
defs.bzl:
def _impl(ctx):
output_dir = ctx.actions.declare_directory("my_outputs")
command = """
mkdir -p {output_dir}
cat > {output_dir}/main.c <<EOF
#include "stdio.h"
#include "mylib.h"
int main() {
printf("hello world %d\\n", get_num());
return 0;
}
EOF
cat > {output_dir}/mylib.c <<EOF
int get_num() {
return 42;
}
EOF
cat > {output_dir}/mylib.h <<EOF
int get_num();
EOF
""".replace("{output_dir}", output_dir.path)
ctx.actions.run_shell(
command = command,
outputs = [output_dir]
)
return [DefaultInfo(files = depset([output_dir])),]
my_rule = rule(
implementation = _impl,
)
usage:
$ bazel run cc
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:cc (15 packages loaded, 57 targets configured).
INFO: Found 1 target...
Target //:cc up-to-date:
bazel-bin/cc
INFO: Elapsed time: 3.626s, Critical Path: 0.06s
INFO: 8 processes: 4 internal, 4 linux-sandbox.
INFO: Build completed successfully, 8 total actions
INFO: Build completed successfully, 8 total actions
hello world 42

how to add a ModulePassManager in llvm pass?

I'm trying to add a ModulePassManager in llvm pass, because all pass is function pass, so all I added createModuleToFunctionPassAdaptor function, but compile error, I don't know how to solve it. does anybody know how to solve this problem. I tried this:
llvm::PreservedAnalyses DBogusFlow::run(llvm::Module &M,
llvm::ModuleAnalysisManager &MAM) {
bool Changed = runOnModule(M);
auto MPM = std::make_unique<ModulePassManager>();
MPM->addPass(createModuleToFunctionPassAdaptor(createAggressiveDCEPass()));
MPM->addPass(
createModuleToFunctionPassAdaptor(createInstructionCombiningPass()));
MPM->addPass(
createModuleToFunctionPassAdaptor(createCFGSimplificationPass()));
MPM->run(M, MAM);
return (Changed ? llvm::PreservedAnalyses::none()
: llvm::PreservedAnalyses::all());
}
below is the compile error
[ 50%] Building CXX object CMakeFiles/DBogusFlow.dir/lib/DBogusFlow.cpp.o
In file included from /usr/include/llvm/IR/PassManager.h:48,
from /home/v4kst1z/Desktop/ollvm-deobfuscator/lib/../include/DBogusFlow.h:12,
from /home/v4kst1z/Desktop/ollvm-deobfuscator/lib/DBogusFlow.cpp:9:
/usr/include/llvm/IR/PassManagerInternal.h: In instantiation of ‘PreservedAnalysesT llvm::detail::PassModel<IRUnitT, PassT, PreservedAnalysesT, AnalysisManagerT, ExtraArgTs>::run(IRUnitT&, AnalysisManagerT&, ExtraArgTs ...) [with IRUnitT = llvm::Function; PassT = llvm::FunctionPass*; PreservedAnalysesT = llvm::PreservedAnalyses; AnalysisManagerT = llvm::AnalysisManager<llvm::Function>; ExtraArgTs = {}]’:
/usr/include/llvm/IR/PassManagerInternal.h:83:22: required from here
/usr/include/llvm/IR/PassManagerInternal.h:85:17: error: request for member ‘run’ in ‘((llvm::detail::PassModel<llvm::Function, llvm::FunctionPass*, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >*)this)->llvm::detail::PassModel<llvm::Function, llvm::FunctionPass*, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::Pass’, which is of pointer type ‘llvm::FunctionPass*’ (maybe you meant to use ‘->’ ?)
85 | return Pass.run(IR, AM, ExtraArgs...);
| ~~~~~^~~
/usr/include/llvm/IR/PassManagerInternal.h: In instantiation of ‘llvm::StringRef llvm::detail::PassModel<IRUnitT, PassT, PreservedAnalysesT, AnalysisManagerT, ExtraArgTs>::name() const [with IRUnitT = llvm::Function; PassT = llvm::FunctionPass*; PreservedAnalysesT = llvm::PreservedAnalyses; AnalysisManagerT = llvm::AnalysisManager<llvm::Function>; ExtraArgTs = {}]’:
/usr/include/llvm/IR/PassManagerInternal.h:88:13: required from here
/usr/include/llvm/IR/PassManagerInternal.h:88:55: error: ‘name’ is not a member of ‘llvm::FunctionPass*’
88 | StringRef name() const override { return PassT::name(); }
| ~~~~~~~~~~~^~
make[2]: *** [CMakeFiles/DBogusFlow.dir/build.make:76: CMakeFiles/DBogusFlow.dir/lib/DBogusFlow.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:86: CMakeFiles/DBogusFlow.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Use Fitnesse symbol to inject baseUrl into RestFixture

I'd like to set the baseUrl for a table based on the outcome of another test (on the same page).
I tried following these pages of Fitnesse's docs (and other resources) :
smartrics blog post, fitnesse symbols page,
but I can't seem to get it working.
So far I've tried with the following syntaxes :
| Fit Rest Fixture | %emailLink% |
| GET | / | 200 |Content-Type: text/plain|Email Verified|
| Fit Rest Fixture | emailLink= |
| GET | / | 200 |Content-Type: text/plain|Email Verified|
| Fit Rest Fixture | $emailLink |
| GET | / | 200 |Content-Type: text/plain|Email Verified|
but none of those work.
I know that the emailLink symbol is not null because I'm testing it in another table, but I can't seem to inject it into the RestFixture.
I always get an IllegalArgumentException indicating that the symbol name has not been resolved against its value, e.g.
java.lang.IllegalArgumentException: Malformed base URL: $emailLink
Any help would be appreciated.
Are you using slim?
http://www.fitnesse.org/FitNesse.UserGuide.WritingAcceptanceTests.SliM.SymbolsInTables
I have used symbols in this way a few time with Slim, but not the REST Fixture specifically.
By taking a look at the code of FitRestFixture and fiddling with it, I've come up with something that works for me.
It seems that the feature I was looking for is not supported out of the box, but can be easily achieved (although this way is not the cleanest) with a simple mod such as the following :
/**
* #return Process args ({#link fit.Fixture}) for Fit runner to extract the
* baseUrl of each Rest request, first parameter of each RestFixture
* table.
*/
protected String getBaseUrlFromArgs() {
String arg = null;
if (args.length > 0) {
arg = args[0];
/* mod starts here */
if (isSymbol(arg)) {
String symbolName = stripSymbolNotation(arg);
arg = resolveSymbol(symbolName);
}
/* mod ends here */
}
return arg;
}
private boolean isSymbol(String arg) {
// notice that I've used the '<<' notation convention to extract the
// the value from a symbol, while in RestFixture the conventional
// notation is %symbolName%
return null != arg && arg.startsWith("<<");
}
private String stripSymbolNotation(String arg) {
return arg.substring(2);
}
private String resolveSymbol(String arg) {
String symbolValue = (String) Fixture.getSymbol(arg);
LOG.warn(String.format("resolved symbol %s to value %s", arg, symbolValue));
return symbolValue;
}

How do I use the Rust parser (libsyntax) myself?

I want to use the Rust parser (libsyntax) to parse a Rust file and extract information like function names out of it. I started digging in the docs and code, so my first goal is a program that prints all function names of freestanding functions in a .rs file.
The program should expand all macros before it prints the function names, so functions declared via macro aren't missed. That's why I can't write some crappy little parser by myself to do the job.
I have to admit that I'm not yet perfectly good at programming Rust, so I apologize in advance for any stupid statements in this question.
How I understood it I need to do the following steps:
Parse the file via the Parser struct
Expand macros with MacroExpander
???
Use a Visitor to walk the AST and extract the information I need (eg. via visit_fn)
So here are my questions:
How do I use MacroExpander?
How do I walk the expanded AST with a custom visitor?
I had the idea of using a custom lint check instead of a fully fledged parser. I'm investigating this option.
If it matters, I'm using rustc 0.13.0-nightly (f168c12c5 2014-10-25 20:57:10 +0000)
You can use syntex to parse Rust, so you don't need to use unstable Rust.
Here's a simple example:
// Tested against syntex_syntax v0.33
extern crate syntex_syntax as syntax;
use std::rc::Rc;
use syntax::codemap::{CodeMap};
use syntax::errors::{Handler};
use syntax::errors::emitter::{ColorConfig};
use syntax::parse::{self, ParseSess};
fn main() {
let codemap = Rc::new(CodeMap::new());
let tty_handler =
Handler::with_tty_emitter(ColorConfig::Auto, None, true, false, codemap.clone());
let parse_session = ParseSess::with_span_handler(tty_handler, codemap.clone());
let src = "fn foo(x: i64) { let y = x + 1; return y; }".to_owned();
let result = parse::parse_crate_from_source_str(String::new(), src, Vec::new(), &parse_session);
println!("parse result: {:?}", result);
}
This prints the whole AST:
parse result: Ok(Crate { module: Mod { inner: Span { lo: BytePos(0), hi: BytePos(43), expn_id: ExpnId(4294967295) },
items: [Item { ident: foo#0, attrs: [], id: 4294967295, node: Fn(FnDecl { inputs: [Arg { ty: type(i64), pat:
pat(4294967295: x), id: 4294967295 }], output: Default(Span { lo: BytePos(15), hi: BytePos(15), expn_id: ExpnId(4294967295) }),
variadic: false }, Normal, NotConst, Rust, Generics { lifetimes: [], ty_params: [], where_clause: WhereClause { id:
4294967295, predicates: [] } }, Block { stmts: [stmt(4294967295: let y = x + 1;), stmt(4294967295: return y;)], expr:
None, id: 4294967295, rules: Default, span: Span { lo: BytePos(15), hi: BytePos(43), expn_id: ExpnId(4294967295) } }),
vis: Inherited, span: Span { lo: BytePos(0), hi: BytePos(43), expn_id: ExpnId(4294967295) } }] }, attrs: [], config: [],
span: Span { lo: BytePos(0), hi: BytePos(42), expn_id: ExpnId(4294967295) }, exported_macros: [] })
I'm afraid I can't answer your question directly; but I can present an alternative that might help.
If all you need is the AST, you can retrieve it in JSON format using rustc -Z ast-json. Then use your favorite language (Python is great) to process the output.
You can also get pretty-printed source using rustc --pretty=(expanded|normal|typed).
For example, given this hello.rs:
fn main() {
println!("hello world");
}
We get:
$ rustc -Z ast-json hello.rs
{"module":{"inner":null,"view_items":[{"node":{"va... (etc.)
$ rustc --pretty=normal hello.rs
#![no_std]
#[macro_use]
extern crate "std" as std;
#[prelude_import]
use std::prelude::v1::*;
fn main() { println!("hello world"); }
$ rustc --pretty=expanded hello.rs
#![no_std]
#[macro_use]
extern crate "std" as std;
#[prelude_import]
use std::prelude::v1::*;
fn main() {
::std::io::stdio::println_args(::std::fmt::Arguments::new({
#[inline]
#[allow(dead_code)]
static __STATIC_FMTSTR:
&'static [&'static str]
=
&["hello world"];
__STATIC_FMTSTR
},
&match () {
() => [],
}));
}
If you need more than that though, a lint plugin would be the best option. Properly handling macro expansion, config flags, the module system, and anything else that comes up is quite non-trivial. With a lint plugin, you get the type-checked AST right away without fuss. Cargo supports compiler plugins too, so your tool will fit nicely into other people's projects.
The syn crate works indeed. At the beginning I wrongly think it is for writing procedural macros (as its readme suggests), but indeed it can parse a source code file. Please look at this page: https://docs.rs/syn/1.0.77/syn/struct.File.html . It even gives an example that inputs a .rs file and output AST (of course, you can do anything with it - not just printing):
use std::env;
use std::fs::File;
use std::io::Read;
use std::process;
fn main() {
let mut args = env::args();
let _ = args.next(); // executable name
let filename = match (args.next(), args.next()) {
(Some(filename), None) => filename,
_ => {
eprintln!("Usage: dump-syntax path/to/filename.rs");
process::exit(1);
}
};
let mut file = File::open(&filename).expect("Unable to open file");
let mut src = String::new();
file.read_to_string(&mut src).expect("Unable to read file");
let syntax = syn::parse_file(&src).expect("Unable to parse file");
// Debug impl is available if Syn is built with "extra-traits" feature.
println!("{:#?}", syntax);
}
Thanks #poolie for pointing out this hint (though lacking a bit of details).
syntex seems to no longer be maintained (last updated 2017), but https://crates.io/crates/syn may do what you need.

Resources