diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index 59f2005490..a126b398a9 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -38,6 +38,8 @@ class TwigEnvironment extends \Twig_Environment {
 
   protected $twigCachePrefix = '';
 
+  protected $templateClassPrefix = '__TwigTemplate_';
+
   /**
    * Constructs a TwigEnvironment object and stores cache and storage
    * internally.
@@ -55,7 +57,7 @@ class TwigEnvironment extends \Twig_Environment {
    * @param array $options
    *   The options for the Twig environment.
    */
-  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
+  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, array $options = []) {
     $this->state = $state;
 
     // Ensure that twig.engine is loaded, given that it is needed to render a
@@ -73,11 +75,6 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension
     ];
     // Ensure autoescaping is always on.
     $options['autoescape'] = 'html';
-
-    $policy = new TwigSandboxPolicy();
-    $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
-    $this->addExtension($sandbox);
-
     if ($options['cache'] === TRUE) {
       $current = $state->get(static::CACHE_PREFIX_METADATA_KEY, ['twig_extension_hash' => '']);
       if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) {
@@ -94,8 +91,11 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension
       $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix);
     }
 
-    $this->loader = $loader;
-    parent::__construct($this->loader, $options);
+    $this->setLoader($loader);
+    parent::__construct($this->getLoader(), $options);
+    $policy = new TwigSandboxPolicy();
+    $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
+    $this->addExtension($sandbox);
   }
 
   /**
@@ -138,7 +138,7 @@ public function getTemplateClass($name, $index = NULL) {
     // node.html.twig for the output of each node and the same compiled class.
     $cache_index = $name . (NULL === $index ? '' : '_' . $index);
     if (!isset($this->templateClasses[$cache_index])) {
-      $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
+      $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->getLoader()->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
     }
     return $this->templateClasses[$cache_index];
   }
@@ -168,7 +168,7 @@ public function getTemplateClass($name, $index = NULL) {
   public function renderInline($template_string, array $context = []) {
     // Prefix all inline templates with a special comment.
     $template_string = '{# inline_template_start #}' . $template_string;
-    return Markup::create($this->loadTemplate($template_string, NULL)->render($context));
+    return Markup::create($this->createTemplate($template_string)->render($context));
   }
 
 }
diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
index 96f55607f3..4c1741ba5e 100644
--- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
+++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
@@ -55,14 +55,14 @@ public function parse(\Twig_Token $token) {
   /**
    * Detect a 'plural' switch or the end of a 'trans' tag.
    */
-  public function decideForFork($token) {
+  public function decideForFork(\Twig_Token $token) {
     return $token->test(['plural', 'endtrans']);
   }
 
   /**
    * Detect the end of a 'trans' tag.
    */
-  public function decideForEnd($token) {
+  public function decideForEnd(\Twig_Token $token) {
     return $token->test('endtrans');
   }
 
diff --git a/core/modules/system/tests/modules/twig_loader_test/src/Loader/TestLoader.php b/core/modules/system/tests/modules/twig_loader_test/src/Loader/TestLoader.php
index 7937daa14d..bd23809e26 100644
--- a/core/modules/system/tests/modules/twig_loader_test/src/Loader/TestLoader.php
+++ b/core/modules/system/tests/modules/twig_loader_test/src/Loader/TestLoader.php
@@ -2,10 +2,21 @@
 
 namespace Drupal\twig_loader_test\Loader;
 
+use Twig\Source;
+
 /**
  * A test Twig loader.
  */
-class TestLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface {
+class TestLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getSourceContext($name) {
+    $name = (string) $name;
+    $value = $this->getSource($name);
+    return new Source($value, $name);
+  }
 
   /**
    * {@inheritdoc}
diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
index 911b6e150d..4191eb01ab 100644
--- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
@@ -280,7 +280,7 @@ public function testTwigAddRemoveClasses($template, $expected, $seed_attributes
    */
   public function providerTestAttributeClassHelpers() {
     return [
-      ["{{ attributes.class }}", ''],
+      ["{{ attributes }}", ''],
       ["{{ attributes.addClass('everest').class }}", 'everest'],
       ["{{ attributes.addClass(['k2', 'kangchenjunga']).class }}", 'k2 kangchenjunga'],
       ["{{ attributes.addClass('lhotse', 'makalu', 'cho-oyu').class }}", 'lhotse makalu cho-oyu'],
diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
index 6cfb562d25..ddd38b8fc2 100644
--- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
@@ -10,6 +10,20 @@
 use Drupal\Core\Template\TwigExtension;
 use Drupal\Core\Url;
 use Drupal\Tests\UnitTestCase;
+/*
+if (!function_exists(__NAMESPACE__ . 't')) {
+
+  function t($string, array $args = []) {
+    return strtr($string, $args);
+  }
+
+}
+if (!function_exists(__NAMESPACE__ . 'file_create_url')) {
+
+  function file_create_url() {
+  }
+
+}*/
 
 /**
  * Tests the twig extension.
@@ -331,7 +345,8 @@ public function testRenderVarWithGeneratedLink() {
    * @covers ::createAttribute
    */
   public function testCreateAttribute() {
-    $loader = new StringLoader();
+    $name = '__string_template_test_1__';
+    $loader = new \Twig_Loader_Array([$name => "{% for iteration in iterations %}<div{{ create_attribute(iteration) }}></div>{% endfor %}"]);
     $twig = new \Twig_Environment($loader);
     $twig->addExtension($this->systemUnderTest);
 
@@ -340,12 +355,15 @@ public function testCreateAttribute() {
       ['id' => 'puppies', 'data-value' => 'foo', 'data-lang' => 'en'],
       [],
     ];
-    $result = $twig->render("{% for iteration in iterations %}<div{{ create_attribute(iteration) }}></div>{% endfor %}", ['iterations' => $iterations]);
+    $result = $twig->render($name, ['iterations' => $iterations]);
     $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div>';
     $this->assertEquals($expected, $result);
 
     // Test default creation of empty attribute object and using its method.
-    $result = $twig->render("<div{{ create_attribute().addClass('meow') }}></div>");
+    $name = '__string_template_test_2__';
+    $loader = new \Twig_Loader_Array([$name => "<div{{ create_attribute().addClass('meow') }}></div>"]);
+    $twig->setLoader($loader);
+    $result = $twig->render($name);
     $expected = '<div class="meow"></div>';
     $this->assertEquals($expected, $result);
   }
