Skip to content

Remove the notion of an "unknown format" #9535

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,12 @@ impl<'self> fmt::Default for Sidebar<'self> {
write!(w, "<a class='{ty} {class}' href='{curty, select,
mod{../}
other{}
}{ty, select,
}{tysel, select,
mod{{name}/index.html}
other{#.{name}.html}
}'>{name}</a><br/>",
ty = short,
tysel = short,
class = class,
curty = shortty(cur),
name = item.as_slice());
Expand Down
19 changes: 8 additions & 11 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::vec;

#[deriving(Eq)]
enum ArgumentType {
Unknown,
Known(@str),
Unsigned,
String,
Expand Down Expand Up @@ -153,14 +152,13 @@ impl Context {
parse::ArgumentIs(i) => Left(i),
parse::ArgumentNamed(s) => Right(s.to_managed()),
};
let ty = if arg.format.ty == "" {
Unknown
} else { Known(arg.format.ty.to_managed()) };
self.verify_arg_type(pos, ty);

// and finally the method being applied
match arg.method {
None => {}
None => {
let ty = Known(arg.format.ty.to_managed());
self.verify_arg_type(pos, ty);
}
Some(ref method) => { self.verify_method(pos, *method); }
}
}
Expand Down Expand Up @@ -253,7 +251,7 @@ impl Context {
return;
}
self.verify_same(self.args[arg].span, ty, self.arg_types[arg]);
if ty != Unknown || self.arg_types[arg].is_none() {
if self.arg_types[arg].is_none() {
self.arg_types[arg] = Some(ty);
}
}
Expand All @@ -269,7 +267,7 @@ impl Context {
};
self.verify_same(span, ty,
self.name_types.find(&name).map(|&x| *x));
if ty != Unknown || !self.name_types.contains_key(&name) {
if !self.name_types.contains_key(&name) {
self.name_types.insert(name, ty);
}
// Assign this named argument a slot in the arguments array if
Expand All @@ -292,9 +290,8 @@ impl Context {
/// that: `Some(None) == Some(Some(x))`
fn verify_same(&self, sp: Span, ty: ArgumentType,
before: Option<ArgumentType>) {
if ty == Unknown { return }
let cur = match before {
Some(Unknown) | None => return,
None => return,
Some(t) => t,
};
if ty == cur { return }
Expand Down Expand Up @@ -649,9 +646,9 @@ impl Context {
};

let fmt_trait = match ty {
Unknown => "Default",
Known(tyname) => {
match tyname.as_slice() {
"" => "Default",
"?" => "Poly",
"b" => "Bool",
"c" => "Char",
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/ifmt-bad-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ fn main() {
// format strings because otherwise the "internal pointer of which argument
// is next" would be invalidated if different cases had different numbers of
// arguments.
format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
format!("{1, select, other{{}}}", 1, "a"); //~ ERROR: cannot use implicit
format!("{1, plural, other{{}}}", 1, 1); //~ ERROR: cannot use implicit
format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit

format!("foo } bar"); //~ ERROR: unmatched `}` found
Expand Down
5 changes: 2 additions & 3 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub fn main() {
t!(format!("{1} {0}", 0, 1), "1 0");
t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
t!(format!("{} {0:s}", "a"), "a a");
t!(format!("{} {0}", "a"), "a a");
t!(format!("{foo_bar}", foo_bar=1), "1");

Expand All @@ -98,8 +97,8 @@ pub fn main() {
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
t!(format!("{1, select, a{#{0:s}} other{#{1}}}", "b", "a"), "ab");
t!(format!("{1, select, a{#{0}} other{#{1}}}", "c", "b"), "bb");
t!(format!("{1, select, a{#{0:s}} other{#}}", "b", "a"), "ab");
t!(format!("{1, select, a{#{0}} other{#}}", "c", "b"), "b");

// Formatting strings and their arguments
t!(format!("{:s}", "a"), "a");
Expand Down