Skip to content

Commit

Permalink
auto merge of #9535 : alexcrichton/rust/no-format-default, r=thestinger
Browse files Browse the repository at this point in the history
As mentioned in #9456, the format! syntax extension would previously consider an
empty format as a 'Unknown' format which could then also get coerced into a
different style of format on another argument.

This is unusual behavior because `{}` is a very common format and if you have
`{0} {0:?}` you wouldn't expect them both to be coereced to the `Poly`
formatter. This commit removes this coercion, but still retains the requirement
that each argument has exactly one format specified for it (an empty format now
counts as well).

Perhaps at a later date we can add support for multiple formats of one argument,
but this puts us in at least a backwards-compatible situation if we decide to do
that.
  • Loading branch information
bors committed Sep 27, 2013
2 parents 10e7f12 + b74663e commit 78c3fac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,11 +1159,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

0 comments on commit 78c3fac

Please sign in to comment.