Skip to content

Commit 76daa0c

Browse files
author
Jakub Bukaj
committed
rollup merge of #18921: oli-obk/refactoring/graphviz/id/new/result_instead_of_fail
creating a new Id object requires the format to match a subset of `ID` format defined by the DOT language. When the format did not match, the function called assert. This was not mentioned in the docs or the spec. I made the failure explicit by returning an Result<Id, ()>.
2 parents db4d60a + 70bf4f7 commit 76daa0c

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/libgraphviz/lib.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ pub fn render_to<W:Writer>(output: &mut W) {
6060
}
6161
6262
impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
63-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1") }
63+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }
6464
6565
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
66-
dot::Id::new(format!("N{}", *n))
66+
dot::Id::new(format!("N{}", *n)).unwrap()
6767
}
6868
}
6969
@@ -163,9 +163,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
163163
}
164164
165165
impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
166-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2") }
166+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
167167
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
168-
dot::Id::new(format!("N{}", n))
168+
dot::Id::new(format!("N{}", n)).unwrap()
169169
}
170170
fn node_label<'a>(&'a self, n: &Nd) -> dot::LabelText<'a> {
171171
dot::LabelStr(str::Slice(self.nodes[*n].as_slice()))
@@ -219,9 +219,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
219219
}
220220
221221
impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
222-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3") }
222+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
223223
fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
224-
dot::Id::new(format!("N{:u}", n.val0()))
224+
dot::Id::new(format!("N{:u}", n.val0())).unwrap()
225225
}
226226
fn node_label<'a>(&'a self, n: &Nd<'a>) -> dot::LabelText<'a> {
227227
let &(i, _) = n;
@@ -354,14 +354,22 @@ impl<'a> Id<'a> {
354354
/// defined by the DOT language. This function may change in the
355355
/// future to accept a broader subset, or the entirety, of DOT's
356356
/// `ID` format.)
357-
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Id<'a> {
357+
///
358+
/// Passing an invalid string (containing spaces, brackets,
359+
/// quotes, ...) will return an empty `Err` value.
360+
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Result<Id<'a>, ()> {
358361
let name = name.into_maybe_owned();
359362
{
360363
let mut chars = name.as_slice().chars();
361-
assert!(is_letter_or_underscore(chars.next().unwrap()));
362-
assert!(chars.all(is_constituent));
364+
match chars.next() {
365+
Some(c) if is_letter_or_underscore(c) => { ; },
366+
_ => return Err(())
367+
}
368+
if !chars.all(is_constituent) {
369+
return Err(());
370+
}
363371
}
364-
return Id{ name: name };
372+
return Ok(Id{ name: name });
365373

366374
fn is_letter_or_underscore(c: char) -> bool {
367375
in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_'
@@ -627,12 +635,12 @@ mod tests {
627635
}
628636

629637
fn id_name<'a>(n: &Node) -> Id<'a> {
630-
Id::new(format!("N{:u}", *n))
638+
Id::new(format!("N{:u}", *n)).unwrap()
631639
}
632640

633641
impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraph {
634642
fn graph_id(&'a self) -> Id<'a> {
635-
Id::new(self.name.as_slice())
643+
Id::new(self.name.as_slice()).unwrap()
636644
}
637645
fn node_id(&'a self, n: &Node) -> Id<'a> {
638646
id_name(n)
@@ -825,4 +833,22 @@ r#"digraph syntax_tree {
825833
}
826834
"#);
827835
}
836+
837+
#[test]
838+
fn simple_id_construction() {
839+
let id1 = dot::Id::new("hello");
840+
match id1 {
841+
Ok(_) => {;},
842+
Err(_) => panic!("'hello' is not a valid value for id anymore")
843+
}
844+
}
845+
846+
#[test]
847+
fn badly_formatted_id() {
848+
let id2 = dot::Id::new("Weird { struct : ure } !!!");
849+
match id2 {
850+
Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"),
851+
Err(_) => {;}
852+
}
853+
}
828854
}

src/librustc/middle/cfg/graphviz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ fn replace_newline_with_backslash_l(s: String) -> String {
5050
}
5151

5252
impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
53-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()) }
53+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()).unwrap() }
5454

5555
fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
56-
dot::Id::new(format!("N{:u}", i.node_id()))
56+
dot::Id::new(format!("N{:u}", i.node_id())).unwrap()
5757
}
5858

5959
fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {

0 commit comments

Comments
 (0)