-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
"source trait is private" error with writeln!
only when println!
isn't used
#27669
Comments
Running $ rustc -Z unstable-options --pretty=expanded src/lib.rs
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
extern crate term;
fn foo() {
let mut t = term::stdout().unwrap();
t.write_fmt(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&["foo\n"];
__STATIC_FMTSTR
},
&match () {
() => [],
})).unwrap();
} Running it on the third example gives this: $ rustc -Z unstable-options --pretty=expanded src/lib.rs
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
extern crate term;
fn foo() {
let mut t = term::stdout().unwrap();
t.write_fmt(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&["foo\n"];
__STATIC_FMTSTR
},
&match () {
() => [],
})).unwrap();
::std::io::_print(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&["foo\n"];
__STATIC_FMTSTR
},
&match () { () => [], }));
} |
My first example was incorrect, this code compiles: use std::fmt::Write; // I had forgot this previously
fn foo() {
let mut s = String::new();
writeln!(s, "foo").unwrap();
} Original issue is unchanged. |
@petrochenkov you mentioned above that this is fixed, what's left to do here then? Just tests? |
Yep! |
Ok. Consider it done. |
So I thought the test that was needed was something that used an external crate, and then uses a trait without importing it from that crate. Steve already posted something using only the standard library and that seems to have made it into a test, but somehow it's not necessary any more to import the trait? use std::thread;
// use std::any::TypeId; // <- actually compiles as-is
fn main() {
let child = thread::spawn(|| {
});
child.join().unwrap_err().get_type_id();
} The test is here (and indeed the In the same vein, the originally failing example now compiles: extern crate term;
fn main() {
let mut t = term::stdout().unwrap();
writeln!(t, "foo").unwrap();
} so does extern crate term;
fn foo(t: &mut std::io::Write) {
t.flush().unwrap();
}
fn main() {
let mut t = term::stdout().unwrap();
foo(&mut t);
} but then this one still errors even though to my untrained eye it looks exactly like the //use std::fmt::Write; // intentional omission
fn main() {
let mut s = String::new();
writeln!(s, "foo").unwrap();
}
/*
error: no method named `write_fmt` found for type `std::string::String` in the current scope
--> main.rs:5:5
|
5 | writeln!(s, "foo").unwrap();
| ^^^^^^^^^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
= help: candidate #1: `use std::fmt::Write;`
= note: this error originates in a macro outside of the current crate
*/ I'm obviously a bit confused, so whatever reading you can throw at me, please do. Perhaps |
@tschottdorf The difference between the |
Gotcha, thanks for the clarification. |
This error is related to #22050, not sure if it's a dupe. Some of the following code snippets rely on the term library (but the issue seems to be in Rust, not
term
).The following builds fine:
This gives an error:
Output:
This is the error mentioned in #22050. Importing
std::io::Write
fixes the error. However, ifprintln
is used, we get no errors:The text was updated successfully, but these errors were encountered: