Repository: toml-rs/toml (language: rust)

Issue:
Redefined tables are allowed when dotted tables exist.
The following TOML:

```toml
[dependencies.foo]
version = "0.16"

[dependencies]
libc = "0.2"

[dependencies]
rand = "0.3.14"
```

seems to parse by the new `toml` implementation when it probably shouldn't. The `[dependencies]` table is defined twice, which goes against the spec per:

> Like keys, you cannot define a table more than once. Doing so is invalid.

This was previously rejected in toml 0.5.

Using toml 0.7.1



Discussion / hints:

Fix patch:
diff --git a/crates/toml_edit/src/parser/state.rs b/crates/toml_edit/src/parser/state.rs
index 299e39f7..efa884d2 100644
--- a/crates/toml_edit/src/parser/state.rs
+++ b/crates/toml_edit/src/parser/state.rs
@@ -117,6 +117,8 @@ impl ParseState {
 
         self.current_table_position += 1;
         self.current_table.decor = decor;
+        self.current_table.set_implicit(false);
+        self.current_table.set_dotted(false);
         self.current_table.set_position(self.current_table_position);
         self.current_table.span = Some(span);
         self.current_is_array = true;
@@ -152,6 +154,8 @@ impl ParseState {
 
         self.current_table_position += 1;
         self.current_table.decor = decor;
+        self.current_table.set_implicit(false);
+        self.current_table.set_dotted(false);
         self.current_table.set_position(self.current_table_position);
         self.current_table.span = Some(span);
         self.current_is_array = false;


Test patch:
diff --git a/crates/toml_edit/tests/testsuite/invalid.rs b/crates/toml_edit/tests/testsuite/invalid.rs
index 8627d1ee..cb13b4e7 100644
--- a/crates/toml_edit/tests/testsuite/invalid.rs
+++ b/crates/toml_edit/tests/testsuite/invalid.rs
@@ -43,6 +43,32 @@ expected `]`
     );
 }
 
+#[test]
+fn duplicate_table_after_dotted_key_issue_509() {
+    let err = "
+[dependencies.foo]
+version = \"0.16\"
+
+[dependencies]
+libc = \"0.2\"
+
+[dependencies]
+rand = \"0.3.14\"
+"
+    .parse::<toml_edit::Document>()
+    .unwrap_err();
+    snapbox::assert_eq(
+        r#"TOML parse error at line 8, column 1
+  |
+8 | [dependencies]
+  | ^
+invalid table header
+duplicate key `dependencies` in document root
+"#,
+        err.to_string(),
+    );
+}
+
 #[test]
 fn bad() {
     let toml_input = "a = 01";