 core/modules/comment/src/CommentForm.php                | 11 ++++++++++-
 core/modules/comment/src/Tests/CommentAnonymousTest.php |  7 ++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php
index 95629b2..8f7368a 100644
--- a/core/modules/comment/src/CommentForm.php
+++ b/core/modules/comment/src/CommentForm.php
@@ -94,6 +94,10 @@ public function form(array $form, FormStateInterface $form_state) {
       $form['#attributes']['data-user-info-from-browser'] = TRUE;
     }
 
+    // Vary per role, because we check a permission above and attach an asset
+    // library only for authenticated users.
+    $form['#cache']['contexts'][] = 'user.roles';
+
     // If not replying to a comment, use our dedicated page callback for new
     // Comments on entities.
     if (!$comment->id() && !$comment->hasParentComment()) {
@@ -210,7 +214,12 @@ public function form(array $form, FormStateInterface $form_state) {
       '#access' => $is_admin,
     );
 
-    $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [],  $config->getCacheTags());
+    $form['#cache']['tags'] = Cache::mergeTags(
+      isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [],
+      $config->getCacheTags(),
+      // The form depends on the field definition.
+      $field_definition->getConfig($entity->bundle())->getCacheTags()
+    );
 
     return parent::form($form, $form_state, $comment);
   }
diff --git a/core/modules/comment/src/Tests/CommentAnonymousTest.php b/core/modules/comment/src/Tests/CommentAnonymousTest.php
index 7b140c1..49ad78c 100644
--- a/core/modules/comment/src/Tests/CommentAnonymousTest.php
+++ b/core/modules/comment/src/Tests/CommentAnonymousTest.php
@@ -37,7 +37,7 @@ protected function setUp() {
    */
   function testAnonymous() {
     $this->drupalLogin($this->adminUser);
-    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
+    $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT);
     $this->drupalLogout();
 
     // Post anonymous comment without contact info.
@@ -46,7 +46,7 @@ function testAnonymous() {
 
     // Allow contact info.
     $this->drupalLogin($this->adminUser);
-    $this->setCommentAnonymous('1');
+    $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
 
     // Attempt to edit anonymous comment.
     $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
@@ -59,6 +59,7 @@ function testAnonymous() {
     $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
 
     // Check the presence of expected cache tags.
+    $this->assertCacheTag('config:field.field.node.article.comment');
     $this->assertCacheTag('config:user.settings');
 
     $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
@@ -78,7 +79,7 @@ function testAnonymous() {
 
     // Require contact info.
     $this->drupalLogin($this->adminUser);
-    $this->setCommentAnonymous('2');
+    $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT);
     $this->drupalLogout();
 
     // Try to post comment with contact info (required).
