Skip to content

Commit 78c3fac

Browse files
committed
auto merge of #9535 : alexcrichton/rust/no-format-default, r=thestinger
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.
2 parents 10e7f12 + b74663e commit 78c3fac

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

src/librustdoc/html/render.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,11 +1159,12 @@ impl<'self> fmt::Default for Sidebar<'self> {
11591159
write!(w, "<a class='{ty} {class}' href='{curty, select,
11601160
mod{../}
11611161
other{}
1162-
}{ty, select,
1162+
}{tysel, select,
11631163
mod{{name}/index.html}
11641164
other{#.{name}.html}
11651165
}'>{name}</a><br/>",
11661166
ty = short,
1167+
tysel = short,
11671168
class = class,
11681169
curty = shortty(cur),
11691170
name = item.as_slice());

src/libsyntax/ext/format.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use std::vec;
2222

2323
#[deriving(Eq)]
2424
enum ArgumentType {
25-
Unknown,
2625
Known(@str),
2726
Unsigned,
2827
String,
@@ -153,14 +152,13 @@ impl Context {
153152
parse::ArgumentIs(i) => Left(i),
154153
parse::ArgumentNamed(s) => Right(s.to_managed()),
155154
};
156-
let ty = if arg.format.ty == "" {
157-
Unknown
158-
} else { Known(arg.format.ty.to_managed()) };
159-
self.verify_arg_type(pos, ty);
160155

161156
// and finally the method being applied
162157
match arg.method {
163-
None => {}
158+
None => {
159+
let ty = Known(arg.format.ty.to_managed());
160+
self.verify_arg_type(pos, ty);
161+
}
164162
Some(ref method) => { self.verify_method(pos, *method); }
165163
}
166164
}
@@ -253,7 +251,7 @@ impl Context {
253251
return;
254252
}
255253
self.verify_same(self.args[arg].span, ty, self.arg_types[arg]);
256-
if ty != Unknown || self.arg_types[arg].is_none() {
254+
if self.arg_types[arg].is_none() {
257255
self.arg_types[arg] = Some(ty);
258256
}
259257
}
@@ -269,7 +267,7 @@ impl Context {
269267
};
270268
self.verify_same(span, ty,
271269
self.name_types.find(&name).map(|&x| *x));
272-
if ty != Unknown || !self.name_types.contains_key(&name) {
270+
if !self.name_types.contains_key(&name) {
273271
self.name_types.insert(name, ty);
274272
}
275273
// Assign this named argument a slot in the arguments array if
@@ -292,9 +290,8 @@ impl Context {
292290
/// that: `Some(None) == Some(Some(x))`
293291
fn verify_same(&self, sp: Span, ty: ArgumentType,
294292
before: Option<ArgumentType>) {
295-
if ty == Unknown { return }
296293
let cur = match before {
297-
Some(Unknown) | None => return,
294+
None => return,
298295
Some(t) => t,
299296
};
300297
if ty == cur { return }
@@ -649,9 +646,9 @@ impl Context {
649646
};
650647

651648
let fmt_trait = match ty {
652-
Unknown => "Default",
653649
Known(tyname) => {
654650
match tyname.as_slice() {
651+
"" => "Default",
655652
"?" => "Poly",
656653
"b" => "Bool",
657654
"c" => "Char",

src/test/compile-fail/ifmt-bad-arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ fn main() {
6565
// format strings because otherwise the "internal pointer of which argument
6666
// is next" would be invalidated if different cases had different numbers of
6767
// arguments.
68-
format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
69-
format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
68+
format!("{1, select, other{{}}}", 1, "a"); //~ ERROR: cannot use implicit
69+
format!("{1, plural, other{{}}}", 1, 1); //~ ERROR: cannot use implicit
7070
format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
7171

7272
format!("foo } bar"); //~ ERROR: unmatched `}` found

src/test/run-pass/ifmt.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ pub fn main() {
8585
t!(format!("{1} {0}", 0, 1), "1 0");
8686
t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
8787
t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
88-
t!(format!("{} {0:s}", "a"), "a a");
8988
t!(format!("{} {0}", "a"), "a a");
9089
t!(format!("{foo_bar}", foo_bar=1), "1");
9190

@@ -98,8 +97,8 @@ pub fn main() {
9897
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
9998
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
10099
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
101-
t!(format!("{1, select, a{#{0:s}} other{#{1}}}", "b", "a"), "ab");
102-
t!(format!("{1, select, a{#{0}} other{#{1}}}", "c", "b"), "bb");
100+
t!(format!("{1, select, a{#{0:s}} other{#}}", "b", "a"), "ab");
101+
t!(format!("{1, select, a{#{0}} other{#}}", "c", "b"), "b");
103102

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

0 commit comments

Comments
 (0)