Index: src/wp-includes/class-wp-theme.php
===================================================================
--- src/wp-includes/class-wp-theme.php	(revision 40901)
+++ src/wp-includes/class-wp-theme.php	(working copy)
@@ -267,6 +267,12 @@
 			}
 		}
 
+		if ( ! $this->template && $this->stylesheet === $this->headers['Template']) {
+			$this->errors = new WP_Error( 'theme_child_invalid', __( 'The theme defines itself as its parent theme. Please fix the "Template" header.' ) );
+			$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet ) );
+			return;
+		}//end if
+
 		// (If template is set from cache [and there are no errors], we know it's good.)
 		if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) {
 			$this->template = $this->stylesheet;
Index: tests/phpunit/data/themedir1/child-parent-itself/style.css
===================================================================
--- tests/phpunit/data/themedir1/child-parent-itself/style.css	(revision 0)
+++ tests/phpunit/data/themedir1/child-parent-itself/style.css	(working copy)
@@ -0,0 +1,4 @@
+/*
+Theme Name: Child Theme
+Template: child-parent-itself
+*/

Property changes on: tests/phpunit/data/themedir1/child-parent-itself/style.css
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: tests/phpunit/tests/theme/WPTheme.php
===================================================================
--- tests/phpunit/tests/theme/WPTheme.php	(revision 40901)
+++ tests/phpunit/tests/theme/WPTheme.php	(working copy)
@@ -140,6 +140,16 @@
 		$this->assertFalse( $theme->display( 'Tags' ) );
 	}
 
+	/**
+	 * @ticket 40820
+	 */
+	function test_child_theme_with_itself_as_parent_should_appear_as_broken() {
+		$theme = new WP_Theme( 'child-parent-itself', $this->theme_root );
+		$errors = $theme->errors();
+		$this->assertWPError( $errors );
+		$this->assertEquals( 'theme_child_invalid', $errors->get_error_code() );
+	}
+
 
 	/**
 	 * Enable a single theme on a network.
@@ -217,4 +227,5 @@
 
 		$this->assertEqualSetsWithIndex( $allowed_themes, $new_allowed_themes );
 	}
+
 }
